parse_pages - improvements to categories, pdf targeting

This commit is contained in:
mDuo13
2015-10-22 13:54:54 -07:00
parent bde7d17f70
commit 718814ee2e
25 changed files with 174 additions and 140 deletions

View File

@@ -30,10 +30,13 @@ PDF_TEMPLATE_FILE = "template-forpdf.html"
PAGE_MANIFEST_FILE = "pages.json"
BUILD_PATH = ".."
CONTENT_PATH = "../content"
BUTTONIZE_FILTER = "buttonize.py"
#BUTTONIZE_FILTER = "buttonize.py"
PRINCE_PAGE_MANIFEST_FILE = "/tmp/devportal-pages.txt"
def parse_markdown(md, environment="local", pages=None):
PDF_TARGET = "pdf"
DEFAULT_TARGET = "local"
def parse_markdown(md, target=DEFAULT_TARGET, pages=None):
## Python markdown requires markdown="1" on HTML block elements
## that contain markdown. AND there's a bug where if you use
## markdown.extensions.extra, it replaces code fences in HTML
@@ -75,19 +78,19 @@ def parse_markdown(md, environment="local", pages=None):
print("done")
#Replace links for live site
if environment != "local":
print("modifying links for environment",environment)
if target != DEFAULT_TARGET:
print("modifying links for target",target)
if not pages:
pages = get_pages()
links = soup.find_all("a",href=re.compile("^[^.]+\.html"))
for link in links:
for page in pages:
if environment in page:
if target in page:
#There's a replacement link for this env
if page["html"] in link["href"]:
link["href"] = link["href"].replace(page["html"],
page[environment])
page[target])
print("done")
print("re-rendering HTML")
@@ -96,15 +99,26 @@ def parse_markdown(md, environment="local", pages=None):
print("done")
return html2
def get_pages():
def get_pages(target=None):
print("reading page manifest...")
with open(PAGE_MANIFEST_FILE) as f:
pages = json.load(f)
if target:
#filter pages that aren't part of this target
pages = [page for page in pages
if "targets" not in page or target in page["targets"]
]
print("done")
return pages
def render_pages(precompiled, pdf=False, environment="local"):
pages = get_pages()
def render_pages(precompiled, target=DEFAULT_TARGET):
pages = get_pages(target)
categories = []#ordered, de-duplicated list
for page in pages:
if "category" in page and page["category"] not in categories:
categories.append(page["category"])
print("categories:",categories)
env = Environment(loader=FileSystemLoader(os.path.curdir))
env.lstrip_blocks = True
@@ -122,18 +136,18 @@ def render_pages(precompiled, pdf=False, environment="local"):
# template_text = f.read()
# doc_template = Template(template_text)
doc_template = env.get_template(DOC_TEMPLATE_FILE)
if pdf:
if target == PDF_TARGET:
doc_template = env.get_template(PDF_TEMPLATE_FILE)
print("done")
if precompiled:
filein = os.path.join(CONTENT_PATH, currentpage["md"])
print("parsing markdown for", currentpage)
print("parsing markdown for", currentpage["name"])
## New markdown module way
with open(filein) as f:
s = f.read()
doc_html = parse_markdown(s, environment, pages)
doc_html = parse_markdown(s, target, pages)
# ## Old Pandoc way
# args = ['pandoc', filein, '-F', BUTTONIZE_FILTER, '-t', 'html']
@@ -143,6 +157,7 @@ def render_pages(precompiled, pdf=False, environment="local"):
print("rendering page",currentpage["name"],"...")
out_html = doc_template.render(currentpage=currentpage,
categories=categories,
pages=pages,
content=doc_html,
precompiled=precompiled)
@@ -153,6 +168,7 @@ def render_pages(precompiled, pdf=False, environment="local"):
print("rendering page",currentpage["name"],"...")
out_html = doc_template.render(currentpage=currentpage,
categories=categories,
pages=pages,
content="",
precompiled=precompiled)
@@ -161,15 +177,14 @@ def render_pages(precompiled, pdf=False, environment="local"):
else:
# Not a documentation page
print("reading template file...")
# with open(currentpage["template"]) as f:
# template_text = f.read()
# template = Template(template_text)
template = env.get_template(currentpage["template"])
print("done")
print("rendering page",currentpage["name"],"...")
out_html = template.render(currentpage=currentpage, pages=pages)
out_html = template.render(currentpage=currentpage,
categories=categories,
pages=pages)
print("done")
@@ -183,14 +198,15 @@ def render_pages(precompiled, pdf=False, environment="local"):
print("done")
def watch(pre_parse, pdf, environment):
def watch(pre_parse, pdf, target):
path = ".."
class UpdaterHandler(PatternMatchingEventHandler):
def on_any_event(self, event):
print("got event!")
if pdf:
make_pdf(pdf)
render_pages(pre_parse, pdf, environment)
render_pages(pre_parse, target)
print("done rendering")
patterns = ["*tool/pages.json","*tool/template-*.html"]
if pre_parse:
@@ -210,24 +226,26 @@ def watch(pre_parse, pdf, environment):
def make_pdf(outfile):
print("rendering PDF-able versions of pages...")
render_pages(True, pdf=outfile)
render_pages(precompiled=True, target=PDF_TARGET)
print("done")
args = ['prince', '-o', outfile, "../index.html"]
pages = get_pages()
args += ["../"+p["html"] for p in pages if "md" in p]
args = ['prince', '-o', outfile]
pages = get_pages(PDF_TARGET)
args += ["../"+p["html"] for p in pages]
print("generating PDF: running ", " ".join(args),"...")
prince_resp = subprocess.check_output(args, universal_newlines=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Generate static site from markdown and templates.')
parser = argparse.ArgumentParser(
description='Generate static site from markdown and templates.')
parser.add_argument("-p", "--pre_parse", action="store_true",
help="Parse markdown; otherwise, use Flatdoc")
parser.add_argument("-w","--watch", action="store_true",
help="Watch for changes and re-generate the files. This runs until force-quit.")
parser.add_argument("--pdf", type=str, help="Generate a PDF, too. Requires Prince.")
parser.add_argument("--environment", "-e", type=str, default="local",
choices=["local","ripple.com"])
parser.add_argument("--pdf", type=str,
help="Generate a PDF, too. Requires Prince.")
parser.add_argument("--target", "-t", type=str, default=DEFAULT_TARGET,
choices=[DEFAULT_TARGET,"ripple.com"])
args = parser.parse_args()
if args.pdf:
@@ -240,9 +258,9 @@ if __name__ == "__main__":
if args.watch:
print("watching for changes...")
watch(args.pre_parse, args.pdf, args.environment)
watch(args.pre_parse, args.pdf, args.target)
else:
print("rendering pages now")
render_pages(args.pre_parse, environment=args.environment)
render_pages(precompiled=args.pre_parse, target=args.target)
print("all done")