|
| 1 | +"""Sphinx Guzzle theme.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import xml.etree.ElementTree as ET |
| 5 | + |
| 6 | +from docutils import nodes |
| 7 | +from sphinx.locale import admonitionlabels |
| 8 | +from sphinx.writers.html import HTMLTranslator as SphinxHTMLTranslator |
| 9 | + |
| 10 | +from pygments.style import Style |
| 11 | +from pygments.token import Keyword, Name, Comment, String, Error, \ |
| 12 | + Number, Operator, Generic, Whitespace, Punctuation, Other, Literal |
| 13 | + |
| 14 | + |
| 15 | +def setup(app): |
| 16 | + """Setup conntects events to the sitemap builder""" |
| 17 | + app.connect('html-page-context', add_html_link) |
| 18 | + app.connect('build-finished', create_sitemap) |
| 19 | + app.sitemap_links = [] |
| 20 | + |
| 21 | + |
| 22 | +def add_html_link(app, pagename, templatename, context, doctree): |
| 23 | + """As each page is built, collect page names for the sitemap""" |
| 24 | + base_url = app.config['html_theme_options'].get('base_url', '') |
| 25 | + if base_url: |
| 26 | + app.sitemap_links.append(base_url + pagename + ".html") |
| 27 | + |
| 28 | + |
| 29 | +def create_sitemap(app, exception): |
| 30 | + """Generates the sitemap.xml from the collected HTML page links""" |
| 31 | + if (not app.config['html_theme_options'].get('base_url', '') or |
| 32 | + exception is not None or |
| 33 | + not app.sitemap_links): |
| 34 | + return |
| 35 | + |
| 36 | + filename = app.outdir + "/sitemap.xml" |
| 37 | + print("Generating sitemap.xml in %s" % filename) |
| 38 | + |
| 39 | + root = ET.Element("urlset") |
| 40 | + root.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9") |
| 41 | + |
| 42 | + for link in app.sitemap_links: |
| 43 | + url = ET.SubElement(root, "url") |
| 44 | + ET.SubElement(url, "loc").text = link |
| 45 | + |
| 46 | + ET.ElementTree(root).write(filename) |
| 47 | + |
| 48 | + |
| 49 | +def html_theme_path(): |
| 50 | + return [os.path.dirname(os.path.abspath(__file__))] |
| 51 | + |
| 52 | + |
| 53 | +class HTMLTranslator(SphinxHTMLTranslator): |
| 54 | + """ |
| 55 | + Handle translating to bootstrap structure. |
| 56 | + """ |
| 57 | + def visit_table(self, node, name=''): |
| 58 | + """ |
| 59 | + Override docutils default table formatter to not include a border |
| 60 | + and to use Bootstrap CSS |
| 61 | + See: http://sourceforge.net/p/docutils/code/HEAD/tree/trunk/docutils/docutils/writers/html4css1/__init__.py#l1550 |
| 62 | + """ |
| 63 | + self.context.append(self.compact_p) |
| 64 | + self.compact_p = True |
| 65 | + classes = ' '.join(['table', 'table-bordered', |
| 66 | + self.settings.table_style]).strip() |
| 67 | + self.body.append( |
| 68 | + self.starttag(node, 'table', CLASS=classes)) |
| 69 | + |
| 70 | + def depart_table(self, node): |
| 71 | + """ |
| 72 | + This needs overridin' too |
| 73 | + """ |
| 74 | + self.compact_p = self.context.pop() |
| 75 | + self.body.append('</table>\n') |
| 76 | + |
| 77 | + |
| 78 | +class GuzzleStyle(Style): |
| 79 | + background_color = "#f8f8f8" |
| 80 | + default_style = "" |
| 81 | + |
| 82 | + styles = { |
| 83 | + # No corresponding class for the following: |
| 84 | + #Text: "", # class: '' |
| 85 | + Whitespace: "underline #f8f8f8", # class: 'w' |
| 86 | + Error: "#a40000 border:#ef2929", # class: 'err' |
| 87 | + Other: "#000000", # class 'x' |
| 88 | + |
| 89 | + Comment: "italic #8f5902", # class: 'c' |
| 90 | + Comment.Preproc: "noitalic", # class: 'cp' |
| 91 | + |
| 92 | + Keyword: "bold #004461", # class: 'k' |
| 93 | + Keyword.Constant: "bold #004461", # class: 'kc' |
| 94 | + Keyword.Declaration: "bold #004461", # class: 'kd' |
| 95 | + Keyword.Namespace: "bold #004461", # class: 'kn' |
| 96 | + Keyword.Pseudo: "bold #004461", # class: 'kp' |
| 97 | + Keyword.Reserved: "bold #004461", # class: 'kr' |
| 98 | + Keyword.Type: "bold #004461", # class: 'kt' |
| 99 | + |
| 100 | + Operator: "#582800", # class: 'o' |
| 101 | + Operator.Word: "bold #004461", # class: 'ow' - like keywords |
| 102 | + |
| 103 | + Punctuation: "bold #000000", # class: 'p' |
| 104 | + |
| 105 | + # because special names such as Name.Class, Name.Function, etc. |
| 106 | + # are not recognized as such later in the parsing, we choose them |
| 107 | + # to look the same as ordinary variables. |
| 108 | + Name: "#000000", # class: 'n' |
| 109 | + Name.Attribute: "#006EC4", # class: 'na' - to be revised |
| 110 | + Name.Builtin: "#004461", # class: 'nb' |
| 111 | + Name.Builtin.Pseudo: "#3465a4", # class: 'bp' |
| 112 | + Name.Class: "#000000", # class: 'nc' - to be revised |
| 113 | + Name.Constant: "#000000", # class: 'no' - to be revised |
| 114 | + Name.Decorator: "#888", # class: 'nd' - to be revised |
| 115 | + Name.Entity: "#ce5c00", # class: 'ni' |
| 116 | + Name.Exception: "bold #cc0000", # class: 'ne' |
| 117 | + Name.Function: "#000000", # class: 'nf' |
| 118 | + Name.Property: "#000000", # class: 'py' |
| 119 | + Name.Label: "#f57900", # class: 'nl' |
| 120 | + Name.Namespace: "#000000", # class: 'nn' - to be revised |
| 121 | + Name.Other: "#000000", # class: 'nx' |
| 122 | + Name.Tag: "bold #004461", # class: 'nt' - like a keyword |
| 123 | + Name.Variable: "#000000", # class: 'nv' - to be revised |
| 124 | + Name.Variable.Class: "#000000", # class: 'vc' - to be revised |
| 125 | + Name.Variable.Global: "#000000", # class: 'vg' - to be revised |
| 126 | + Name.Variable.Instance: "#000000", # class: 'vi' - to be revised |
| 127 | + |
| 128 | + Number: "#990000", # class: 'm' |
| 129 | + |
| 130 | + Literal: "#000000", # class: 'l' |
| 131 | + Literal.Date: "#000000", # class: 'ld' |
| 132 | + |
| 133 | + String: "#4e9a06", # class: 's' |
| 134 | + String.Backtick: "#4e9a06", # class: 'sb' |
| 135 | + String.Char: "#4e9a06", # class: 'sc' |
| 136 | + String.Doc: "italic #8f5902", # class: 'sd' - like a comment |
| 137 | + String.Double: "#4e9a06", # class: 's2' |
| 138 | + String.Escape: "#4e9a06", # class: 'se' |
| 139 | + String.Heredoc: "#4e9a06", # class: 'sh' |
| 140 | + String.Interpol: "#4e9a06", # class: 'si' |
| 141 | + String.Other: "#4e9a06", # class: 'sx' |
| 142 | + String.Regex: "#4e9a06", # class: 'sr' |
| 143 | + String.Single: "#4e9a06", # class: 's1' |
| 144 | + String.Symbol: "#4e9a06", # class: 'ss' |
| 145 | + |
| 146 | + Generic: "#000000", # class: 'g' |
| 147 | + Generic.Deleted: "#a40000", # class: 'gd' |
| 148 | + Generic.Emph: "italic #000000", # class: 'ge' |
| 149 | + Generic.Error: "#ef2929", # class: 'gr' |
| 150 | + Generic.Heading: "bold #000080", # class: 'gh' |
| 151 | + Generic.Inserted: "#00A000", # class: 'gi' |
| 152 | + Generic.Output: "#888", # class: 'go' |
| 153 | + Generic.Prompt: "#745334", # class: 'gp' |
| 154 | + Generic.Strong: "bold #000000", # class: 'gs' |
| 155 | + Generic.Subheading: "bold #800080", # class: 'gu' |
| 156 | + Generic.Traceback: "bold #a40000", # class: 'gt' |
| 157 | + } |
0 commit comments