Skip to content

Commit

Permalink
custom html templates
Browse files Browse the repository at this point in the history
  • Loading branch information
dwrolvink committed Oct 21, 2021
1 parent 1369984 commit 99dde82
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
4 changes: 4 additions & 0 deletions example_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ site_name: 'Obsidian-Html/Notes'
# use '/html_prefix' (prepend slash, no slash at the end)
html_url_prefix: ''

# Provide the fullpath to a template file to use instead of standard template.
# Note that this file must contain at least "{content}" somewhere in the page.
html_template_path_str: ''

toggles:
# Opt-in/-out of Obsidian->Md conversion, set to False when using proper markdown as input
compile_md: True
Expand Down
43 changes: 34 additions & 9 deletions obsidianhtml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ def ConvertMarkdownPageToHtmlPage(page_path_str, paths, files, html_template, co
def main():
# Config
# ------------------------------------------
if '-h' in sys.argv or len(sys.argv) < 3:
print('[Obsidian-html]')
print('- Add -i </path/to/input.yml> to provide config')
print('- Add -v for verbose output')
print('- Add -h to get helptext')
print('- Add -eht <target/path/file.name> to export the html template.')
exit()

# Functions other than main function
export_html_template_target_path = None
for i, v in enumerate(sys.argv):
if v == '-eht':
if len(sys.argv) < (i + 2):
raise Exception("No output path given.\n Use obsidianhtml -eht /target/path/to/template.html to provide input.")
exit(1)
export_html_template_target_path = Path(sys.argv[i+1]).resolve()
export_html_template_target_path.parent.mkdir(parents=True, exist_ok=True)
html = pkg_resources.read_text(src, 'template.html')
with open (export_html_template_target_path, 'w', encoding="utf-8") as t:
t.write(html)
print(f"Exported html template to {str(export_html_template_target_path)}.")
exit(0)

# Load input yaml
input_yml_path_str = ''
for i, v in enumerate(sys.argv):
Expand All @@ -221,13 +244,6 @@ def main():

# Input
# ------------------------------------------
if '-h' in sys.argv or len(sys.argv) < 3:
print('[Obsidian-html]')
print('- Add -i </path/to/input.yml> to provide config\n')
print('- Add -v for verbose output')
print('- Add -h to get helptext')
exit()

# Set Paths
paths = {
'obsidian_folder': Path(conf['obsidian_folder_path_str']).resolve(),
Expand All @@ -241,7 +257,7 @@ def main():
paths['rel_obsidian_entrypoint'] = paths['obsidian_entrypoint'].relative_to(paths['obsidian_folder'])
paths['rel_md_entrypoint_path'] = paths['md_entrypoint'].relative_to(paths['md_folder'])

print(yaml.dump(conf, allow_unicode=True, default_flow_style=False))
#print(yaml.dump(conf, allow_unicode=True, default_flow_style=False))

# Preprocess
# ------------------------------------------
Expand Down Expand Up @@ -287,7 +303,16 @@ def main():

# Get html template code. Every note will become a html page, where the body comes from the note's
# markdown, and the wrapper code from this template.
html_template = pkg_resources.read_text(src, 'template.html')
if 'html_template_path_str' in conf.keys() and conf['html_template_path_str'] != '':
print('-------------')
with open(Path(conf['html_template_path_str']).resolve()) as f:
html_template = f.read()
else:
html_template = pkg_resources.read_text(src, 'template.html')

if '{content}' not in html_template:
raise Exception('The provided html template does not contain the string `{content}`. This will break its intended use as a template.')
exit(1)

# Load all filenames in the markdown folder
# This data is used to check which links are local
Expand Down
2 changes: 2 additions & 0 deletions pypi_readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ To convert your notes, you need to point to your notes folder, and to one note t
Only notes that are found by following links recursively, starting with the entrypoint, will be converted!

**Changelog**:
0.0.4: Added the option to use a custom html template, and to export the packaged template.
0.0.3: Updated readme file to work with pypi.
0.0.2: Updated readme file to work with pypi.
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ To view the html as a website, do the following:
- Run `python -m http.server --dir /path/to/your/html/output/folder`
- Open [http://localhost:8000](http://localhost:8000)

## Use a custom html template
By default, obsidianhtml will use its packaged html template to compile the html output.
To change this, first export the packaged template to a given path:

``` bash
obsidianhtml -eht C:\Users\User\Downloads\template.html
```

Then, open and edit your exported template. Note that the string '{content}' should remain present somewhere in the template.
Finally, pass the path to your custom template as an input variable. Open your config.yml, and fill in the full path under `html_template_path_str`.

This will make sure future runs of obsidianhtml will use your custom template (provided that you use that specific yaml file as input).

# Features
## Not supported
- Tags (you can use them in Obsidian, but they are ignored in the conversion)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = obsidianhtml
version = 0.0.3
version = 0.0.4
summary = Converts Obsidian notes into proper markdown and HTML
long_description = file: pypi_readme.md
home-page = https://github.com/obsidian-html/obsidian-html
Expand Down

0 comments on commit 99dde82

Please sign in to comment.