Skip to content

Commit

Permalink
feat: support expand env vars and home user in path_tempfile
Browse files Browse the repository at this point in the history
This allows you specify paths with `~` and variables like `$HOME`:

    "path_tempfile": "~/.cache/markdownpreview"
  • Loading branch information
gerardroche committed Dec 17, 2023
1 parent b3524bf commit 22e3cb4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions MarkdownPreview.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions docs/src/markdown/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions markdown_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 22e3cb4

Please sign in to comment.