parse_pages - different output for different environments

This commit is contained in:
mDuo13
2015-10-21 13:05:46 -07:00
parent 58630c3f77
commit 9c2a89e412
3 changed files with 83 additions and 27 deletions

View File

@@ -3,60 +3,70 @@
"name": "rippled", "name": "rippled",
"md":"rippled.md", "md":"rippled.md",
"html":"rippled-apis.html", "html":"rippled-apis.html",
"ripple.com": "https://ripple.com/build/rippled-apis/",
"type": "reference" "type": "reference"
}, },
{ {
"name": "rippled Setup", "name": "rippled Setup",
"md":"rippled-setup.md", "md":"rippled-setup.md",
"html":"rippled-setup.html", "html":"rippled-setup.html",
"ripple.com": "https://ripple.com/build/rippled-setup/",
"type": "tutorial" "type": "tutorial"
}, },
{ {
"name": "Ripple-REST", "name": "Ripple-REST",
"md": "ripplerest_api.md", "md": "ripplerest_api.md",
"html": "ripple-rest.html", "html": "ripple-rest.html",
"ripple.com": "https://ripple.com/build/ripple-rest/",
"type": "reference" "type": "reference"
}, },
{ {
"name": "Transactions", "name": "Transactions",
"md":"tx_format.md", "md":"tx_format.md",
"html":"transactions.html", "html":"transactions.html",
"ripple.com": "https://ripple.com/build/transactions/",
"type": "reference" "type": "reference"
}, },
{ {
"name": "Ripple Consensus Ledger", "name": "Ripple Consensus Ledger",
"md":"ledger_format.md", "md":"ledger_format.md",
"html":"ripple-ledger.html", "html":"ripple-ledger.html",
"ripple.com": "https://ripple.com/build/ledger-format/",
"type": "reference" "type": "reference"
}, },
{ {
"name": "Reliable Transaction Submission", "name": "Reliable Transaction Submission",
"md":"reliable_tx.md", "md":"reliable_tx.md",
"html":"reliable_tx.html", "html":"reliable_tx.html",
"ripple.com": "https://ripple.com/build/reliable-transaction-submission/",
"type": "tutorial" "type": "tutorial"
}, },
{ {
"name": "Gateway Guide", "name": "Gateway Guide",
"md":"gateway_guide.md", "md":"gateway_guide.md",
"html":"gateway_guide.html", "html":"gateway_guide.html",
"ripple.com": "https://ripple.com/build/gateway-guide/",
"type": "tutorial" "type": "tutorial"
}, },
{ {
"name": "Historical Data API", "name": "Historical Data API",
"md": "historical_data.md", "md": "historical_data.md",
"html": "historical_data.html", "html": "historical_data.html",
"ripple.com": "https://ripple.com/build/historical-database/",
"type": "reference" "type": "reference"
}, },
{ {
"name": "Ripple Charts API", "name": "Ripple Charts API",
"md": "charts_api.md", "md": "charts_api.md",
"html": "charts_api.html", "html": "charts_api.html",
"ripple.com": "https://ripple.com/build/charts-api/",
"type": "reference" "type": "reference"
}, },
{ {
"name": "Ripple Data API v2", "name": "Ripple Data API v2",
"md": "data_v2.md", "md": "data_v2.md",
"html": "data_api_v2.html", "html": "data_api_v2.html",
"ripple.com": "https://ripple.com/build/data-api-v2/",
"type": "reference" "type": "reference"
}, },
{ {
@@ -75,6 +85,7 @@
"name": "Transfer Fees", "name": "Transfer Fees",
"md":"transferrate.md", "md":"transferrate.md",
"html":"transfer_fees.html", "html":"transfer_fees.html",
"ripple.com": "https://ripple.com/knowledge_center/transfer-fees/",
"type": "reference" "type": "reference"
}, },
{ {
@@ -94,6 +105,7 @@
"methods_js": "js/apitool-methods-ripplerest.js", "methods_js": "js/apitool-methods-ripplerest.js",
"is_apitool": true, "is_apitool": true,
"rest_host": "https://api.ripple.com", "rest_host": "https://api.ripple.com",
"ripple.com": "https://ripple.com/build/rest-tool/",
"html": "rest-api-tool.html" "html": "rest-api-tool.html"
}, },
{ {
@@ -102,12 +114,14 @@
"methods_js": "js/apitool-methods-historical.js", "methods_js": "js/apitool-methods-historical.js",
"type": "apitool", "type": "apitool",
"rest_host": "https://history.ripple.com", "rest_host": "https://history.ripple.com",
"ripple.com": "https://ripple.com/build/historical-database-api-tool/",
"html": "historicaldb-api-tool.html" "html": "historicaldb-api-tool.html"
}, },
{ {
"name": "WebSocket API Tool", "name": "WebSocket API Tool",
"template":"template-ripple-api-tool.html", "template":"template-ripple-api-tool.html",
"type": "apitool", "type": "apitool",
"ripple.com": "https://ripple.com/build/websocket-tool/",
"html": "ripple-api-tool.html" "html": "ripple-api-tool.html"
}, },
{ {
@@ -116,6 +130,7 @@
"methods_js": "js/apitool-methods-charts.js", "methods_js": "js/apitool-methods-charts.js",
"type": "apitool", "type": "apitool",
"rest_host": "https://api.ripplecharts.com", "rest_host": "https://api.ripplecharts.com",
"ripple.com": "https://ripple.com/build/charts-api-tool/",
"html": "charts-api-tool.html" "html": "charts-api-tool.html"
}, },
{ {

View File

@@ -33,11 +33,12 @@ CONTENT_PATH = "../content"
BUTTONIZE_FILTER = "buttonize.py" BUTTONIZE_FILTER = "buttonize.py"
PRINCE_PAGE_MANIFEST_FILE = "/tmp/devportal-pages.txt" PRINCE_PAGE_MANIFEST_FILE = "/tmp/devportal-pages.txt"
def parse_markdown(md): def parse_markdown(md, environment="local", pages=None):
## Python markdown requires markdown="1" on HTML block elements ## Python markdown requires markdown="1" on HTML block elements
## that contain markdown. AND there's a bug where if you use ## that contain markdown. AND there's a bug where if you use
## markdown.extensions.extra, it replaces code fences in HTML ## markdown.extensions.extra, it replaces code fences in HTML
## block elements with garbled text ## block elements with garbled text
print("adding markdown class to embedded divs...")
def add_markdown_class(m): def add_markdown_class(m):
if m.group(0).find("markdown=") == -1: if m.group(0).find("markdown=") == -1:
return m.group(1) + ' markdown="1">' return m.group(1) + ' markdown="1">'
@@ -45,18 +46,54 @@ def parse_markdown(md):
return m.group(0) return m.group(0)
md = re.sub("(<div[^>]*)>", add_markdown_class, md) md = re.sub("(<div[^>]*)>", add_markdown_class, md)
print("done")
#the actual markdown parsing is the easy part #the actual markdown parsing is the easy part
html = markdown(md, extensions=["markdown.extensions.extra", "markdown.extensions.toc"]) print("parsing markdown...")
html = markdown(md, extensions=["markdown.extensions.extra",
"markdown.extensions.toc"])
print("done")
#replace underscores with dashes in h1,h2,etc. for Flatdoc compatibility #replace underscores with dashes in h1,h2,etc. for Flatdoc compatibility
print("tweaking header IDs...")
soup = BeautifulSoup(html, "html.parser") soup = BeautifulSoup(html, "html.parser")
headers = soup.find_all(name=re.compile("h[0-9]"), id=True) headers = soup.find_all(name=re.compile("h[0-9]"), id=True)
for h in headers: for h in headers:
if "_" in h["id"]: if "_" in h["id"]:
h["id"] = h["id"].replace("_","-") h["id"] = h["id"].replace("_","-")
print("done")
html2 = soup.prettify() #buttonize links ending in >
print("buttonizing try-it links...")
buttonlinks = soup.find_all("a", string=re.compile(">$"))
## print("buttonlinks:",buttonlinks)
for link in buttonlinks:
if "class" in link.attrs:
link["class"].append("button")
else:
link["class"] = "button"
print("done")
#Replace links for live site
if environment != "local":
print("modifying links for environment",environment)
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:
#There's a replacement link for this env
if page["html"] in link["href"]:
link["href"] = link["href"].replace(page["html"],
page[environment])
print("done")
print("re-rendering HTML")
#html2 = soup.prettify()
html2 = str(soup)
print("done")
return html2 return html2
def get_pages(): def get_pages():
@@ -66,17 +103,9 @@ def get_pages():
print("done") print("done")
return pages return pages
def render_pages(precompiled, pdf=False): def render_pages(precompiled, pdf=False, environment="local"):
pages = get_pages() pages = get_pages()
# if pdf:
# precompiled = True#Prince probably won't work otherwise
# with open(PRINCE_PAGE_MANIFEST_FILE,"w") as f:
# for page in pages:
# if "md" in page:
# f.write(page["html"])
# f.write("\n\n")
env = Environment(loader=FileSystemLoader(os.path.curdir)) env = Environment(loader=FileSystemLoader(os.path.curdir))
env.lstrip_blocks = True env.lstrip_blocks = True
env.trim_blocks = True env.trim_blocks = True
@@ -88,6 +117,7 @@ def render_pages(precompiled, pdf=False):
print("reading template file...") print("reading template file...")
# #Experimental: Preprocessing the doc files using Jinja
# with open(DOC_TEMPLATE_FILE) as f: # with open(DOC_TEMPLATE_FILE) as f:
# template_text = f.read() # template_text = f.read()
# doc_template = Template(template_text) # doc_template = Template(template_text)
@@ -103,7 +133,7 @@ def render_pages(precompiled, pdf=False):
## New markdown module way ## New markdown module way
with open(filein) as f: with open(filein) as f:
s = f.read() s = f.read()
doc_html = parse_markdown(s) doc_html = parse_markdown(s, environment, pages)
# ## Old Pandoc way # ## Old Pandoc way
# args = ['pandoc', filein, '-F', BUTTONIZE_FILTER, '-t', 'html'] # args = ['pandoc', filein, '-F', BUTTONIZE_FILTER, '-t', 'html']
@@ -111,17 +141,21 @@ def render_pages(precompiled, pdf=False):
# doc_html = subprocess.check_output(args, universal_newlines=True) # doc_html = subprocess.check_output(args, universal_newlines=True)
print("done") print("done")
print("rendering page",currentpage,"...") print("rendering page",currentpage["name"],"...")
out_html = doc_template.render(currentpage=currentpage, pages=pages, out_html = doc_template.render(currentpage=currentpage,
content=doc_html, precompiled=precompiled) pages=pages,
content=doc_html,
precompiled=precompiled)
print("done") print("done")
else: else:
print("compiling skipped") print("compiling skipped")
print("rendering page",currentpage,"...") print("rendering page",currentpage["name"],"...")
out_html = doc_template.render(currentpage=currentpage, pages=pages, out_html = doc_template.render(currentpage=currentpage,
content="", precompiled=precompiled) pages=pages,
content="",
precompiled=precompiled)
print("done") print("done")
else: else:
@@ -134,7 +168,7 @@ def render_pages(precompiled, pdf=False):
print("done") print("done")
print("rendering page",currentpage,"...") print("rendering page",currentpage["name"],"...")
out_html = template.render(currentpage=currentpage, pages=pages) out_html = template.render(currentpage=currentpage, pages=pages)
print("done") print("done")
@@ -149,14 +183,14 @@ def render_pages(precompiled, pdf=False):
print("done") print("done")
def watch(pre_parse, pdf): def watch(pre_parse, pdf, environment):
path = ".." path = ".."
class UpdaterHandler(PatternMatchingEventHandler): class UpdaterHandler(PatternMatchingEventHandler):
def on_any_event(self, event): def on_any_event(self, event):
print("got event!") print("got event!")
if pdf: if pdf:
make_pdf(pdf) make_pdf(pdf)
render_pages(pre_parse, pdf) render_pages(pre_parse, pdf, environment)
patterns = ["*tool/pages.json","*tool/template-*.html"] patterns = ["*tool/pages.json","*tool/template-*.html"]
if pre_parse: if pre_parse:
@@ -176,7 +210,7 @@ def watch(pre_parse, pdf):
def make_pdf(outfile): def make_pdf(outfile):
print("rendering PDF-able versions of pages...") print("rendering PDF-able versions of pages...")
render_pages(True, outfile) render_pages(True, pdf=outfile)
print("done") print("done")
args = ['prince', '-o', outfile, "../index.html"] args = ['prince', '-o', outfile, "../index.html"]
@@ -192,16 +226,23 @@ if __name__ == "__main__":
parser.add_argument("-w","--watch", action="store_true", parser.add_argument("-w","--watch", action="store_true",
help="Watch for changes and re-generate the files. This runs until force-quit.") 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("--pdf", type=str, help="Generate a PDF, too. Requires Prince.")
parser.add_argument("--environment", "-e", type=str, default="local",
choices=["local","ripple.com"])
args = parser.parse_args() args = parser.parse_args()
if args.pdf: if args.pdf:
if args.pdf[-4:] != ".pdf": if args.pdf[-4:] != ".pdf":
exit("PDF filename must end in .pdf") exit("PDF filename must end in .pdf")
print("making a pdf...")
make_pdf(args.pdf) make_pdf(args.pdf)
print("pdf done")
#Not an accident that we go on to re-gen files in non-PDF format #Not an accident that we go on to re-gen files in non-PDF format
if args.watch: if args.watch:
watch(args.pre_parse, args.pdf) print("watching for changes...")
watch(args.pre_parse, args.pdf, args.environment)
else: else:
render_pages(args.pre_parse) print("rendering pages now")
render_pages(args.pre_parse, environment=args.environment)
print("all done")

View File

@@ -41,7 +41,7 @@
<script src="js/fixsidebarscroll.js"></script> <script src="js/fixsidebarscroll.js"></script>
{% endblock %} {% endblock %}
{% block bodyattrs %} role='flatdoc' class='no-literate'{% endblock %} {% block bodyattrs %}{% if not precompiled %} role='flatdoc'{% endif %} class='no-literate'{% endblock %}
{% block contentroot %} {% block contentroot %}
<div class='content-root'> <div class='content-root'>
@@ -55,7 +55,7 @@
{% endif %} {% endif %}
</div> </div>
</div> </div>
<div role='flatdoc-content' class='content'> <div {% if not precompiled %}role='flatdoc-content' {% endif %}class='content'>
{% if precompiled %} {% if precompiled %}
{{ content }} {{ content }}
{% endif %} {% endif %}