Skip to content

Commit

Permalink
Allow find and replace metadata and do not error when file not found.
Browse files Browse the repository at this point in the history
  • Loading branch information
person-al committed Jun 24, 2021
1 parent ab779f9 commit 6410c16
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions obyde/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import argparse
import sys
import os
import re
import string
import frontmatter
from collections import defaultdict
Expand Down Expand Up @@ -86,6 +87,25 @@ def generate_post_link(dated_name, post_link_mode):
else:
raise ValueError(f'Unknown post link mode: {post_link_mode}')

def find_replace(content, metadata):
find_list = metadata.get("find")
replace_list = metadata.get("replace")

if not (find_list and replace_list):
return content

if len(find_list) != len(replace_list):
raise ValueError(f'Length of with list is not the same as the length of the replace list')

rewritten = content
for i in range(len(find_list)):
find_regex = re.compile(find_list[i])
replace_string = replace_list[i]
rewritten = re.sub(find_regex,replace_string,rewritten)

print(rewritten)
return rewritten

def rewrite_links(content, dated_file_index, asset_index, relative_asset_path_prefix, post_link_mode):
obsidian_links = parse_obsidian_links(content)
rewritten = content
Expand All @@ -106,9 +126,10 @@ def rewrite_links(content, dated_file_index, asset_index, relative_asset_path_pr
break
if not written:
link_name_slug = slugify_md_filename(link_target)
dated_name, _, _ = dated_file_index[link_name_slug]
rewritten = rewritten.replace(
link, f'[{link_text}]({generate_post_link(dated_name, post_link_mode)})')
dated_name, _, _ = dated_file_index.get(link_name_slug, (None, None, None))
if dated_name:
rewritten = rewritten.replace(
link, f'[{link_text}]({generate_post_link(dated_name, post_link_mode)})')
return rewritten


Expand Down Expand Up @@ -162,6 +183,7 @@ def process_vault(config):
copied_asset_files = write_asset_files(asset_files, asset_output_path)

dated_files = {}
metadata_map = {}
for name, path in md_files.items():
name, ext = os.path.splitext(name)
slug_name = slugify_md_filename(name)
Expand All @@ -170,13 +192,15 @@ def process_vault(config):
dated_name = postdate + '-' + slug_name
dated_name_ext = dated_name + ext
dated_files[slug_name] = (dated_name, dated_name_ext, path)
metadata_map[slug_name] = metadata

for slug_name, data in dated_files.items():
_, dated_name_ext, path = data
with open(path, 'r') as fs:
filedata = fs.read()
rewritten = find_replace(filedata, metadata_map[slug_name])
rewritten = rewrite_links(
filedata, dated_files, copied_asset_files, relative_asset_path_prefix, post_link_mode)
rewritten, dated_files, copied_asset_files, relative_asset_path_prefix, post_link_mode)

with open(os.path.join(post_output_path, dated_name_ext), 'w') as out:
out.write(rewritten)
Expand Down

0 comments on commit 6410c16

Please sign in to comment.