Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support expanding variables and home dir in path_tempfile #175

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading