Skip to content

Instantly share code, notes, and snippets.

@idelem
Created November 30, 2020 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idelem/505344ededb0e68e5b1404a02bf01b50 to your computer and use it in GitHub Desktop.
Save idelem/505344ededb0e68e5b1404a02bf01b50 to your computer and use it in GitHub Desktop.
workflowy site generator
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 {font-family:'Consolas', 'Menlo', 'Helvetica Neue', Arial, sans-serif; color:#333; font-size:13px; line-height:17px; max-width: 100%; margin: 2em auto 2em auto; text-align:center;}
body .name,body .note {white-space:pre-wrap;}
body ul {list-style:disc; margin:0; padding:0;}
body li {margin:auto; 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.2em 0 0 0; display: inline-block; overflow-wrap: break-word; word-break: break-all;}
body summary:focus {outline: none; font-weight: bold;}
a {color:#999;text-decoration:none;}
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;}
summary::-webkit-details-marker {display: none;}
li {background: rgba(0,0,0,0.012); border-top: 1px solid #ccc;}
body > li, body > ul > li {background: #fff;}
.note {text-align: left;}
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 {font-family:'Consolas', 'Menlo', 'Helvetica Neue', Arial, sans-serif; color:#333; font-size:13px; line-height:17px; max-width: 100%; margin: 2em auto 2em auto; text-align:center;}
body .name,body .note {white-space:pre-wrap;}
body ul {list-style:disc; margin:0; padding:0;}
body li {margin:auto; 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.2em 0 0 0; display: inline-block; overflow-wrap: break-word; word-break: break-all;}
body summary:focus {outline: none; font-weight: bold;}
a {color:#999;text-decoration:none;}
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;}
summary::-webkit-details-marker {display: none;}
li {background: rgba(0,0,0,0.012); border-top: 1px solid #ccc;}
body > li, body > ul > li {background: #fff;}
.note {text-align: left;}
# -*- 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):
styleformat = """
<style>
%s
</style>\n"""
# TODO:pass arg
with open("plis.css", 'r', encoding='UTF-8') as f:
stylestr = ''.join(f.readlines())
stylestr = styleformat % (stylestr)
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'<li><span class="name">(.*?)</span></span><br />'
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