From b3524bfd1ec36da504d80cfe8d12063a797a2d8b Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Sun, 17 Dec 2023 11:30:37 +0000 Subject: [PATCH 1/2] refactor: get temp preview path --- markdown_preview.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/markdown_preview.py b/markdown_preview.py index 40e5c66..556ef07 100644 --- a/markdown_preview.py +++ b/markdown_preview.py @@ -90,22 +90,28 @@ def request_url(url, data, headers): def get_temp_preview_path(view): """Return a permanent full path of the temp markdown preview file.""" - settings = sublime.load_settings('MarkdownPreview.sublime-settings') + tmp_dir = get_temp_preview_dir(view) + if not os.path.isdir(tmp_dir): # create directory if not exists + os.makedirs(tmp_dir) tmp_filename = '%s.html' % view.id() - if settings.get('path_tempfile'): - if os.path.isabs(settings.get('path_tempfile')): # absolute path or not - tmp_dir = settings.get('path_tempfile') + tmp_fullpath = os.path.join(tmp_dir, tmp_filename) + return tmp_fullpath + + +def get_temp_preview_dir(view): + """Return a permanent full dir of the temp markdown preview file.""" + settings = sublime.load_settings('MarkdownPreview.sublime-settings') + path_tempfile = settings.get('path_tempfile') + if path_tempfile: + if os.path.isabs(path_tempfile): # absolute path or not + tmp_dir = path_tempfile else: - tmp_dir = os.path.join(os.path.dirname(view.file_name()), settings.get('path_tempfile')) + tmp_dir = os.path.join(os.path.dirname(view.file_name()), path_tempfile) else: tmp_dir = tempfile.gettempdir() - if not os.path.isdir(tmp_dir): # create directory if not exists - os.makedirs(tmp_dir) - - tmp_fullpath = os.path.join(tmp_dir, tmp_filename) - return tmp_fullpath + return tmp_dir def save_utf8(filename, text): From 22e3cb4ba04591959cac1d3a0fca441acc8a6c2e Mon Sep 17 00:00:00 2001 From: Gerard Roche Date: Sun, 17 Dec 2023 11:39:26 +0000 Subject: [PATCH 2/2] feat: support expand env vars and home user in path_tempfile This allows you specify paths with `~` and variables like `$HOME`: "path_tempfile": "~/.cache/markdownpreview" --- MarkdownPreview.sublime-settings | 3 +++ docs/src/markdown/usage.md | 3 +++ markdown_preview.py | 9 +++++++++ 3 files changed, 15 insertions(+) diff --git a/MarkdownPreview.sublime-settings b/MarkdownPreview.sublime-settings index 5d6c0b5..3a3ba1a 100644 --- a/MarkdownPreview.sublime-settings +++ b/MarkdownPreview.sublime-settings @@ -306,6 +306,9 @@ the OS default. The directory will be created if it doesn't exist yet. Relative paths are supported and are checked against `os.path.isabs`, see docs: http://docs.python.org/3/library/os.path.html#os.path.isabs + Environment variables and user "~" placeholder are supported. + see docs: https://docs.python.org/3/library/os.path.html#os.path.expanduser + see docs: https://docs.python.org/3/library/os.path.html#os.path.expandvars Examples: /tmp/custom_folder (Linux/macOS - absolute path) C:/TEMP/MYNOTES diff --git a/docs/src/markdown/usage.md b/docs/src/markdown/usage.md index 697d06f..c31f99a 100644 --- a/docs/src/markdown/usage.md +++ b/docs/src/markdown/usage.md @@ -129,6 +129,9 @@ via the `path_tempfile` option: using LiveReload and don't want to use the OS default. The directory will be created if it doesn't exist. Relative paths are supported, and are checked against `os.path.isabs`, see doc: http://docs.python.org/3/library/os.path.html#os.path.isabs + Environment variables and user "~" placeholder are supported. + see docs: https://docs.python.org/3/library/os.path.html#os.path.expanduser + see docs: https://docs.python.org/3/library/os.path.html#os.path.expandvars Examples: /tmp/custom_folder (Linux/OSX - absolute path) C:/TEMP/MYNOTES diff --git a/markdown_preview.py b/markdown_preview.py index 556ef07..5d81cb4 100644 --- a/markdown_preview.py +++ b/markdown_preview.py @@ -104,6 +104,7 @@ def get_temp_preview_dir(view): settings = sublime.load_settings('MarkdownPreview.sublime-settings') path_tempfile = settings.get('path_tempfile') if path_tempfile: + path_tempfile = filter_path(path_tempfile) if os.path.isabs(path_tempfile): # absolute path or not tmp_dir = path_tempfile else: @@ -114,6 +115,14 @@ def get_temp_preview_dir(view): return tmp_dir +def filter_path(path): + """Return a path with user and variables expanded.""" + path = os.path.expanduser(path) + path = os.path.expandvars(path) + + return path + + def save_utf8(filename, text): """Save to UTF8 file.""" with codecs.open(filename, 'w', encoding='utf-8')as f: