Skip to content

Commit

Permalink
implements obsidian-html#28
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrolvink committed Feb 16, 2022
1 parent 5f48c29 commit 74d9984
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion obsidianhtml/MarkdownLink.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import urllib.parse # convert link characters like %
import warnings
import shutil # used to remove a non-empty directory, copy files
from .lib import DuplicateFileNameInRoot, GetObsidianFilePath, image_suffixes
from .lib import DuplicateFileNameInRoot, GetObsidianFilePath

class MarkdownLink:
"""Helper class to abstract away a lot of recurring path-testing logic."""
Expand Down
8 changes: 4 additions & 4 deletions obsidianhtml/MarkdownPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import urllib.parse # convert link characters like %
import warnings
import shutil # used to remove a non-empty directory, copy files
from .lib import DuplicateFileNameInRoot, GetObsidianFilePath, image_suffixes, ConvertTitleToMarkdownId, MalformedTags
from .lib import DuplicateFileNameInRoot, GetObsidianFilePath, ConvertTitleToMarkdownId, MalformedTags
from .HeaderTree import PrintHeaderTree, ConvertMarkdownToHeaderTree

class MarkdownPage:
Expand Down Expand Up @@ -87,7 +87,7 @@ def AddToTagtree(self, tagtree, url=''):
if n == (len(tag.split('/')) - 1):
ctagtree['notes'].append(url)

def ConvertObsidianPageToMarkdownPage(self, dst_folder_path, entrypoint_path, include_depth=0):
def ConvertObsidianPageToMarkdownPage(self, pb, dst_folder_path, entrypoint_path, include_depth=0):
"""Full subroutine converting the Obsidian Code to proper markdown. Linked files are copied over to the destination folder."""
# -- Load contents
self.SetDestinationPath(dst_folder_path, entrypoint_path)
Expand Down Expand Up @@ -126,7 +126,7 @@ def ConvertObsidianPageToMarkdownPage(self, dst_folder_path, entrypoint_path, in

# Obsidian page inclusions use the same tag...
# Skip if we don't match image suffixes. Inclusions are handled at the end.
if len(link.split('.')) == 1 or link.split('.')[-1].split('|')[0] not in image_suffixes:
if len(link.split('.')) == 1 or link.split('.')[-1].split('|')[0] not in pb.gc('included_file_suffixes'):
new_link = f'<inclusion href="{link}" />'

safe_link = re.escape('![['+link+']]')
Expand Down Expand Up @@ -311,7 +311,7 @@ def ConvertObsidianPageToMarkdownPage(self, dst_folder_path, entrypoint_path, in

# Get code
included_page = MarkdownPage(incl_page_path, self.src_folder_path, self.file_tree)
included_page.ConvertObsidianPageToMarkdownPage(self.dst_folder_path, entrypoint_path, include_depth=include_depth + 1)
included_page.ConvertObsidianPageToMarkdownPage(pb, self.dst_folder_path, entrypoint_path, include_depth=include_depth + 1)

# Get subsection of code if header is present
if header != '':
Expand Down
6 changes: 3 additions & 3 deletions obsidianhtml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from .lib import DuplicateFileNameInRoot, CreateTemporaryCopy, \
GetObsidianFilePath, OpenIncludedFile, ExportStaticFiles, \
IsValidLocalMarkdownLink, PopulateTemplate, \
image_suffixes, printHelpAndExit
printHelpAndExit
from .PicknickBasket import PicknickBasket
from .Feature_CreateIndexFromTags import CreateIndexFromTags

Expand Down Expand Up @@ -50,7 +50,7 @@ def recurseObisidianToMarkdown(page_path_str, pb, log_level=1):
md = MarkdownPage(page_path, paths['obsidian_folder'], files)

# The bulk of the conversion process happens here
md.ConvertObsidianPageToMarkdownPage(paths['md_folder'], paths['obsidian_entrypoint'])
md.ConvertObsidianPageToMarkdownPage(pb, paths['md_folder'], paths['obsidian_entrypoint'])

# The frontmatter was stripped from the obsidian note prior to conversion
# Add yaml frontmatter back in
Expand Down Expand Up @@ -165,7 +165,7 @@ def ConvertMarkdownPageToHtmlPage(page_path_str, pb, backlinkNode=None, log_leve
continue

# [12] Copy non md files over wholesale, then we're done for that kind of file
if link.suffix != '.md' and link.suffix not in image_suffixes:
if link.suffix != '.md' and link.suffix not in pb.gc('included_file_suffixes'):
paths['html_output_folder'].joinpath(link.rel_src_path).parent.mkdir(parents=True, exist_ok=True)
try:
shutil.copyfile(link.src_path, paths['html_output_folder'].joinpath(link.rel_src_path))
Expand Down
3 changes: 0 additions & 3 deletions obsidianhtml/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import importlib.util
from . import src

# Lookup tables
image_suffixes = ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'pdf']

class DuplicateFileNameInRoot(Exception):
pass
class MalformedTags(Exception):
Expand Down
4 changes: 4 additions & 0 deletions obsidianhtml/src/defaults_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ exclude_subfolders:
# The tempdir is automatically removed on exit of the program.
copy_vault_to_tempdir: True

# ObsidianHtml needs to be able to discern between included notes and included files, because included files
# need to be treated differently. This is a configurable setting because we might've missed certain suffixes
# of files that are includable.
included_file_suffixes: ['jpg', 'jpeg', 'gif', 'png', 'bmp', 'pdf']

##########################################################################
# HTML OUTPUT #
Expand Down

0 comments on commit 74d9984

Please sign in to comment.