Support images as well

This commit is contained in:
Jonathan Sundqvist 2021-07-03 20:41:06 +02:00
parent b118883511
commit ad572d265c

View File

@ -28,6 +28,14 @@ link_re = re.compile(link)
""" """
def get_file_and_linkname(match):
group = match.groupdict()
filename = group['filename'].strip()
linkname = group['linkname'] if group['linkname'] else filename
linkname = linkname.strip()
return filename, linkname
class ObsidianMarkdownReader(MarkdownReader): class ObsidianMarkdownReader(MarkdownReader):
""" """
Change the format of various links to the accepted case of pelican. Change the format of various links to the accepted case of pelican.
@ -37,12 +45,8 @@ class ObsidianMarkdownReader(MarkdownReader):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def replace_obsidian_links(self, text): def replace_obsidian_links(self, text):
def replacement(match): def link_replacement(match):
nonlocal self filename, linkname = get_file_and_linkname(match)
group = match.groupdict()
filename = group['filename'].strip()
linkname = group['linkname'] if group['linkname'] else filename
linkname = linkname.strip()
path = ARTICLES.get(filename) path = ARTICLES.get(filename)
if path: if path:
link_structure = '[{linkname}]({{filename}}/{path}/{filename}.md)'.format( link_structure = '[{linkname}]({{filename}}/{path}/{filename}.md)'.format(
@ -52,7 +56,20 @@ class ObsidianMarkdownReader(MarkdownReader):
link_structure = '{linkname}'.format(linkname=linkname) link_structure = '{linkname}'.format(linkname=linkname)
return link_structure return link_structure
text = link_re.sub(replacement, text) def file_replacement(match):
filename, linkname = get_file_and_linkname(match)
path = FILES.get(filename)
if path:
link_structure = '![{linkname}]({{static}}/{path}/{filename})'.format(
linkname=linkname, path=path, filename=filename
)
else:
# don't show it at all since it will be broken
link_structure = ''
return link_structure
text = file_re.sub(file_replacement, text)
text = link_re.sub(link_replacement, text)
return text return text
def read(self, source_path): def read(self, source_path):
@ -88,8 +105,14 @@ def populate_files_and_articles(article_generator):
full_path = str(full_path).replace(str(base_path) + '/', '') full_path = str(full_path).replace(str(base_path) + '/', '')
ARTICLES[filename] = full_path ARTICLES[filename] = full_path
globs = [base_path.glob('**/*.{}'.format(ext)) for ext in ['png', 'jpg', 'svg', 'apkg', 'gif']]
files = chain(*globs)
if not FILES: if not FILES:
pass for _file in files:
full_path, filename_w_ext = os.path.split(_file)
full_path = str(full_path).replace(str(base_path) + '/', '')
FILES[filename_w_ext] = full_path
def modify_reader(article_generator): def modify_reader(article_generator):