Skip to content

Commit

Permalink
Support expanding variables and home dir in path_tempfile (#175)
Browse files Browse the repository at this point in the history
* refactor: get temp preview path

* 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"
  • Loading branch information
gerardroche authored Dec 29, 2023
1 parent a1b327a commit e5508dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 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
33 changes: 24 additions & 9 deletions markdown_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,37 @@ 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:
path_tempfile = filter_path(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)
return tmp_dir

tmp_fullpath = os.path.join(tmp_dir, tmp_filename)
return tmp_fullpath

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):
Expand Down

0 comments on commit e5508dd

Please sign in to comment.