Skip to content
Permalink
Browse files

Switch to mistune for MD rendering, fix up CSS to current version, fi…

…nish the templates, fix up various data processing differences
  • Loading branch information...
cmsj committed Dec 23, 2016
1 parent da62397 commit 7bc817c6106386b61a19ab6b49a1681bd06d2205
@@ -1,3 +1,3 @@
jinja2
misaka
mistune
pygments
@@ -19,6 +19,7 @@
import os
import pprint
import sqlite3
import string
import sys

DEBUG = False
@@ -35,11 +36,16 @@
"Constant": "Useful values which cannot be changed",
"Variable": "Configurable values",
"Function": "API calls offered directly by the extension",
"Method": "API calls which can only be made on an object returned by a constructor",
"Constructor": "API calls which return an object, typically one that offers API methods",
"Method": "API calls which can only be made on an object returned "
"by a constructor",
"Constructor": "API calls which return an object, typically one "
"that offers API methods",
"Command": "External shell commands",
"Field": "Variables which can only be access from an object returned by a constructor",
"Deprecated": "API features which will be removed in an future release"}
"Field": "Variables which can only be access from an object returned "
"by a constructor",
"Deprecated": "API features which will be removed in an future "
"release"}


def dbg(msg):
"""Print a debug message"""
@@ -85,7 +91,10 @@ def extract_docstrings(filename):
chunk.append(filename)
chunk.append("%d" % i)
# Append the line to the current chunk
chunk.append(line.strip("/- "))
line = line.strip("/-")
if len(line) > 0 and line[0] == ' ':
line = line[1:]
chunk.append(line)
else:
# We hit a line that isn't a docstring. If we were previously
# processing docstrings, we just exited a chunk of docs, so
@@ -99,16 +108,32 @@ def extract_docstrings(filename):


def find_module_for_item(modules, item):
"""Find the longest matching module for a given item"""
"""Find the matching module for a given item"""
dbg("find_module_for_item: Searching for: %s" % item)
matches = []
for module in modules:
if item.startswith(module):
matches.append(module)
module = None

# We need a shortcut here for root level items
if string.count(item, '.') == 1:
dbg("find_module_for_item: Using root-level shortcut")
module = "hs"

# Methods are very easy to shortcut
if string.count(item, ':') == 1:
dbg("find_module_for_item: Using method shortcut")
module = item.split(':')[0]

matches.sort()
dbg("find_module_for_item: Found: %s" % matches[-1])
return matches[-1]
if not module:
matches = []
for mod in modules:
if item.startswith(mod):
matches.append(mod)

matches.sort()
dbg("find_module_for_item: Found options: %s" % matches)
module = matches[-1]

dbg("find_module_for_item: Found: %s" % module)
return module


def find_itemname_from_signature(signature):
@@ -123,6 +148,7 @@ def remove_method_from_itemname(itemname):

def find_basename_from_itemname(itemname):
"""Find the base name of an item, from its full name"""
# (where "base name" means the function/method/variable/etc name
splitchar = '.'
if ':' in itemname:
splitchar = ':'
@@ -208,9 +234,7 @@ def process_docstrings(docstrings):
itemname,
chunk[CHUNK_FILE],
chunk[CHUNK_LINE]))
item_name_without_method = remove_method_from_itemname(itemname)
modulename = find_module_for_item(docs.keys(),
item_name_without_method)
modulename = find_module_for_item(docs.keys(), itemname)
dbg("process_docstrings: Assigning item to module: %s" %
modulename)
docs[modulename]["items"][itemname] = chunk
@@ -273,53 +297,47 @@ def process_module(modulename, raw_module):
return module


def strip_paragraph(text):
"""Strip <p> from the start of a string, and </p>\n from the end"""
text = text.replace("<p>", "")
text = text.replace("</p>\n", "")
return text


def process_markdown(data):
"""Pre-render GitHub-flavoured Markdown, and syntax-highlight code"""
import misaka
from misaka import HtmlRenderer
import mistune
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.formatters import html

class HighlighterRenderer(HtmlRenderer):
def block_code(self, text, lang):
s = ''
class HighlightRenderer(mistune.Renderer):
def block_code(self, code, lang):
if not lang:
lang = 'text'
try:
lexer = get_lexer_by_name(lang, stripall=True)
except:
s += '<div class="highlight"><span class="err">Error: ' \
'language "%s" is not supported</span></div>' % lang
lexer = get_lexer_by_name('text', stripall=True)
formatter = HtmlFormatter()
s += highlight(text, lexer, formatter)
return s

def table(self, header, body):
return '<table class="table">\n'+header+'\n'+body+'\n</table>'

renderer = HighlighterRenderer(flags=misaka.HTML_ESCAPE |
misaka.HTML_HARD_WRAP)
md = misaka.Markdown(renderer, extensions=misaka.EXT_FENCED_CODE |
misaka.EXT_NO_INTRA_EMPHASIS | misaka.EXT_TABLES |
misaka.EXT_AUTOLINK | misaka.EXT_SPACE_HEADERS |
misaka.EXT_STRIKETHROUGH | misaka.EXT_SUPERSCRIPT)
return '\n<pre><code>%s</code></pre>\n' % \
mistune.escape(code)
lexer = get_lexer_by_name(lang, stripall=True)
formatter = html.HtmlFormatter()
return highlight(code, lexer, formatter)

renderer = HighlightRenderer()
md = mistune.Markdown(renderer=renderer)

for i in xrange(0, len(data)):
module = data[i]
module["desc_gfm"] = md(module["desc"])
module["doc_gfm"] = md(module["doc"])
for item_type in TYPE_NAMES:
items = module[item_type]
for j in xrange(0, len(items)):
item = items[j]
item["def_gfm"] = md(item["def"])
item["def_gfm"] = strip_paragraph(md(item["def"]))
item["doc_gfm"] = md(item["doc"])
items[j] = item
# Now do the same for the deprecated 'items' list
for j in xrange(0, len(module["items"])):
item = module["items"][j]
item["def_gfm"] = md(item["def"])
item["def_gfm"] = strip_paragraph(md(item["def"]))
item["doc_gfm"] = md(item["doc"])
module["items"][j] = item
data[i] = module
@@ -6,13 +6,14 @@

body {
color: #333;
font-family: system, -apple-system, ".SFNSDisplay-Regular", HelveticaNeue, LucidaGrande, sans-serif;
font-size: 12pt;
font-family: "Helvetica Neue", Helvetica, "Segoe UI", Arial, freesans, sans-serif;
font-size: 13pt;
line-height: 1.6;
word-wrap: break-word;
width: 80%;
margin: 0.5em auto;
padding: 0 30px;
border: 1px solid rgb(221, 221, 221);
padding: 30px;
}

pre {
@@ -27,14 +28,14 @@ pre {
}

code {
font-family: "Ubuntu Mono", Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 12px;
}

pre {
margin-top: 0;
margin-bottom: 0;
font: 12px "Ubuntu Mono", Consolas, "Liberation Mono", Menlo, Courier, monospace;
font: 12px Consolas, "Liberation Mono", Menlo, Courier, monospace;
}

a {
@@ -197,9 +198,6 @@ dd {
margin-left: 0;
}

li {
line-height: 1.3em;
}

.octicon {
font: normal normal 16px octicons-anchor;
@@ -248,7 +246,7 @@ h5,
h6 {
position: relative;
margin-top: 1em;
margin-bottom: 0;
margin-bottom: 16px;
font-weight: bold;
line-height: 1.4;
}
@@ -285,8 +283,7 @@ h6:hover .anchor .octicon-link {
}

h1 {
padding-bottom: 0.1em;
margin:0;
padding-bottom: 0.3em;
font-size: 2.25em;
line-height: 1.2;
border-bottom: 1px solid #eee;
@@ -325,7 +322,7 @@ h4 .anchor {
}

h5 {
font-size: 1.1em;
font-size: 1em;
}

h5 .anchor {
@@ -426,12 +423,6 @@ table td {
border: 1px solid #ddd;
}

/* Narrow to overview here,
otherwise we trample the function descriptions */
table.api-documentation-overview td p {
margin: 0;
}

table tr {
background-color: #fff;
border-top: 1px solid #ccc;
@@ -513,10 +504,6 @@ pre code:after {
content: normal;
}

.documentation-section {
border-bottom: 1px solid lightgray;
}


.pl-c {
color: #969896;
@@ -14,7 +14,41 @@
<h1>Hammerspoon.app</h1>
</header>
<section>
{{ content }}
<h3>Project links</h3>
<table>
<thead>
<tr>
<th>Resource</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr>
<td>Website</td>
<td><a href="http://www.hammerspoon.org/">http://www.hammerspoon.org/</a></td>
</tr>
<tr>
<td>GitHub page</td>
<td><a href="https://github.com/Hammerspoon/hammerspoon/">https://github.com/Hammerspoon/hammerspoon/</a></td>
</tr>
<tr>
<td>Getting Started Guide</td>
<td><a href="http://www.hammerspoon.org/go/">http://www.hammerspoon.org/go/</a></td>
</tr>
<tr>
<td>IRC channel</td>
<td>#hammerspoon on freenode</td>
</tr>
<tr>
<td>Mailing list</td>
<td><a href="https://groups.google.com/forum/#!forum/hammerspoon/">https://groups.google.com/forum/#!forum/hammerspoon/</a></td>
</tr>
<tr>
<td>LuaSkin API docs</td>
<td><a href="http://www.hammerspoon.org/docs/LuaSkin/">http://www.hammerspoon.org/docs/LuaSkin/</a></td>
</tr>
</tbody>
</table>
</section>
<section>
<!-- tables suck., I know, but it's fast to code -->
@@ -22,7 +22,7 @@ <h3>API Overview</h3>
{% for type in type_order %}
{# Considering: {{ type }} ({{ module[type]|length }}) #}
{% if module[type]|length > 0 %}
<li>{{ type }}s - {{ type_desc.type }}</li>
<li>{{ type }}s - {{ type_desc[type] }}</li>
<ul>
{% for item in module[type] %}
<li><a href="#{{ item.name }}">{{ item.name }}</a></li>
@@ -38,6 +38,7 @@ <h4 class="documentation-section">{{ type}}s</h4>
{% for item in module[type] %}
<section id="{{ item.name }}">
<a name="//apple_ref/cpp/{{ item.type }}/{{ item.name }}" class="dashAnchor"></a>
<h5><a href="#{{ item.name }}">{{ item.name }}</a></h5>
<table>
<tr>
<th>Signature</th>

0 comments on commit 7bc817c

Please sign in to comment.
You can’t perform that action at this time.