Last active
November 28, 2020 08:51
-
-
Save idelem/34eaffe04d62cf8577804eaaa344918b to your computer and use it in GitHub Desktop.
Generates static page from workflowy export files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os, re | |
rootdir = '.' | |
def add_mobile_viewport_support(filepath): | |
metastr = ' <meta name="viewport" content="width=device-width, initial-scale=1">\n' | |
lines = [] | |
with open(filepath, 'r', encoding='UTF-8') as f: | |
lines = f.readlines() | |
if metastr in lines: | |
return | |
for i in range(len(lines)): | |
if lines[i].strip(" ").startswith("<meta"): | |
lines.insert(i, metastr) | |
break | |
with open(filepath, 'w', encoding='UTF-8') as f: | |
f.writelines(lines) | |
def add_footer(filepath): | |
footerstr = """ | |
<footer> | |
<p><a href="/search/search.html" target="_self">search</a> | <a href="https://github.com/idelem/trilium-garden" target="_blank">git repo</a></p> | |
</footer>\n""" | |
lines = [] | |
newlines = [] | |
skipping = False | |
with open(filepath, 'r', encoding='UTF-8') as f: | |
lines = f.readlines() | |
for i in range(len(lines)): | |
if "<footer>" in lines[i]: | |
skipping = True | |
if "</footer>" in lines[i]: | |
skipping = False | |
continue | |
if skipping: | |
continue | |
if lines[i].strip(" ").startswith("</body>"): | |
newlines.append(footerstr) | |
newlines.append(lines[i]) | |
with open(filepath, 'w', encoding='UTF-8') as f: | |
f.writelines(newlines) | |
def add_style(filepath): | |
stylestr = """ | |
<style> | |
body {font-family:'Consolas', 'Menlo', 'Helvetica Neue', Arial, sans-serif; color:#333; font-size:13px; line-height:17px; max-width: 600px; margin: 2em auto 2em auto; } | |
@media screen and (max-width: 600px) {body {margin: 2em 1em;}} | |
body .name,body .note {white-space:pre-wrap;} | |
body ul {list-style:disc; margin:0; padding:0;} | |
body li {margin:4px 0 4px 20px; padding:0; list-style: none;} | |
body>.name {font-size:16px; line-height:21px;} | |
body>.note {font-size:13px; line-height:17px;} | |
body>ul {margin-top:15px;} | |
body .name.done {text-decoration:line-through; color:#999;} | |
body .note {font-size:12px; color:#666; padding: 0.5em 0 0.5em 0; display: inline-block; overflow-wrap: break-word; word-break: break-all;} | |
body summary:focus {outline: none; font-weight: bold;} | |
body summary:only-child::-webkit-details-marker {visibility: hidden;} | |
a {color:#999;} | |
a:hover, ::selection { background-color:#000;color:#fff;text-decoration:none;} | |
br {display: none;} | |
body>br {display:inline-block;} | |
body .contentTag {color:#999;} | |
body div {display: inline-block;} | |
</style>\n""" | |
# TODO: read style from css | |
lines = [] | |
newlines = [] | |
skipping = False | |
with open(filepath, 'r', encoding='UTF-8') as f: | |
lines = f.readlines() | |
for i in range(len(lines)): | |
if "<style>" in lines[i]: | |
skipping = True | |
if "</style>" in lines[i]: | |
skipping = False | |
newlines.append(stylestr) | |
continue | |
if skipping: | |
continue | |
newlines.append(lines[i]) | |
with open(filepath, 'w', encoding='UTF-8') as f: | |
f.writelines(newlines) | |
def add_details(filepath): | |
p0 = u'<div class="contentTag" (.*?)>(.*?)<div class="contentTagText">(.*?)</div><div class="contentTagNub">(.*?)</div></div>' | |
r0 = u'<span class="contentTag" \\1>\\2<span class="contentTagText">\\3</span><span class="contentTagNub">\\4</span></span>' | |
p1 = u'<li><div class="name">(.*?)</div></div>' | |
r1 = u'<li><details><summary class="name">\\1</div></summary>' | |
p2 = u'</li>' | |
r2 = u'</details></li>' | |
lines = [] | |
with open(filepath, 'r', encoding='UTF-8') as f: | |
lines = f.readlines() | |
newlines = ''.join(lines) | |
newlines = re.sub('<(.*?)span(.*?)>', '<\\1div\\2>', newlines, flags=re.UNICODE|re.DOTALL) | |
newlines = re.sub(p0, r0, newlines, flags=re.UNICODE|re.DOTALL) | |
newlines = re.sub(p1, r1, newlines, flags=re.UNICODE|re.DOTALL) | |
newlines = re.sub(p2, r2, newlines, flags=re.UNICODE|re.DOTALL) | |
with open(filepath, 'w', encoding='UTF-8') as f: | |
f.writelines(newlines) | |
exclude_dirs = [".git", "src", "search", "node_modules", "flowy", "public"] | |
def is_excluded(subdir): | |
for dirname in exclude_dirs: | |
if dirname in subdir: | |
return True | |
def main(): | |
# TODO allow args | |
for subdir, dirs, files in os.walk(rootdir): | |
if is_excluded(subdir): | |
continue | |
for file in files: | |
filepath = os.path.join(subdir, file) | |
if file.endswith(".html"): | |
print(filepath) | |
add_mobile_viewport_support(filepath) | |
add_style(filepath) | |
add_details(filepath) | |
# add_footer(filepath) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment