Path: blob/develop/doc/source/guzzle_sphinx_theme/__init__.py
1567 views
"""Sphinx Guzzle theme."""12import os3import xml.etree.ElementTree as ET45from docutils import nodes6from sphinx.locale import admonitionlabels7from sphinx.writers.html import HTMLTranslator as SphinxHTMLTranslator89from pygments.style import Style10from pygments.token import Keyword, Name, Comment, String, Error, \11Number, Operator, Generic, Whitespace, Punctuation, Other, Literal121314def setup(app):15"""Setup connects events to the sitemap builder"""16app.connect('html-page-context', add_html_link)17app.connect('build-finished', create_sitemap)18app.sitemap_links = []19app.set_translator('html', HTMLTranslator)202122def add_html_link(app, pagename, templatename, context, doctree):23"""As each page is built, collect page names for the sitemap"""24base_url = app.config['html_theme_options'].get('base_url', '')25if base_url:26app.sitemap_links.append(base_url + pagename + ".html")272829def create_sitemap(app, exception):30"""Generates the sitemap.xml from the collected HTML page links"""31if (not app.config['html_theme_options'].get('base_url', '') or32exception is not None or33not app.sitemap_links):34return3536filename = app.outdir + "/sitemap.xml"37print("Generating sitemap.xml in %s" % filename)3839root = ET.Element("urlset")40root.set("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")4142for link in app.sitemap_links:43url = ET.SubElement(root, "url")44ET.SubElement(url, "loc").text = link4546ET.ElementTree(root).write(filename)474849def html_theme_path():50return [os.path.dirname(os.path.abspath(__file__))]515253class HTMLTranslator(SphinxHTMLTranslator):54def visit_admonition(self, node, name=''):55"""Uses the h3 tag for admonition titles instead of the p tag"""56self.body.append(self.starttag(57node, 'div', CLASS=('admonition ' + name)))58if name:59title = (60f"<h3 class='admonition-title'>"61f"{admonitionlabels[name]}</h3>"62)63self.body.append(title)64self.set_first_last(node)656667class GuzzleStyle(Style):68background_color = "#f8f8f8"69default_style = ""7071styles = {72# No corresponding class for the following:73#Text: "", # class: ''74Whitespace: "underline #f8f8f8", # class: 'w'75Error: "#a40000 border:#ef2929", # class: 'err'76Other: "#000000", # class 'x'7778Comment: "italic #8f5902", # class: 'c'79Comment.Preproc: "noitalic", # class: 'cp'8081Keyword: "bold #004461", # class: 'k'82Keyword.Constant: "bold #004461", # class: 'kc'83Keyword.Declaration: "bold #004461", # class: 'kd'84Keyword.Namespace: "bold #004461", # class: 'kn'85Keyword.Pseudo: "bold #004461", # class: 'kp'86Keyword.Reserved: "bold #004461", # class: 'kr'87Keyword.Type: "bold #004461", # class: 'kt'8889Operator: "#582800", # class: 'o'90Operator.Word: "bold #004461", # class: 'ow' - like keywords9192Punctuation: "bold #000000", # class: 'p'9394# because special names such as Name.Class, Name.Function, etc.95# are not recognized as such later in the parsing, we choose them96# to look the same as ordinary variables.97Name: "#000000", # class: 'n'98Name.Attribute: "#006EC4", # class: 'na' - to be revised99Name.Builtin: "#004461", # class: 'nb'100Name.Builtin.Pseudo: "#3465a4", # class: 'bp'101Name.Class: "#000000", # class: 'nc' - to be revised102Name.Constant: "#000000", # class: 'no' - to be revised103Name.Decorator: "#888", # class: 'nd' - to be revised104Name.Entity: "#ce5c00", # class: 'ni'105Name.Exception: "bold #cc0000", # class: 'ne'106Name.Function: "#000000", # class: 'nf'107Name.Property: "#000000", # class: 'py'108Name.Label: "#f57900", # class: 'nl'109Name.Namespace: "#000000", # class: 'nn' - to be revised110Name.Other: "#000000", # class: 'nx'111Name.Tag: "bold #004461", # class: 'nt' - like a keyword112Name.Variable: "#000000", # class: 'nv' - to be revised113Name.Variable.Class: "#000000", # class: 'vc' - to be revised114Name.Variable.Global: "#000000", # class: 'vg' - to be revised115Name.Variable.Instance: "#000000", # class: 'vi' - to be revised116117Number: "#990000", # class: 'm'118119Literal: "#000000", # class: 'l'120Literal.Date: "#000000", # class: 'ld'121122String: "#4e9a06", # class: 's'123String.Backtick: "#4e9a06", # class: 'sb'124String.Char: "#4e9a06", # class: 'sc'125String.Doc: "italic #8f5902", # class: 'sd' - like a comment126String.Double: "#4e9a06", # class: 's2'127String.Escape: "#4e9a06", # class: 'se'128String.Heredoc: "#4e9a06", # class: 'sh'129String.Interpol: "#4e9a06", # class: 'si'130String.Other: "#4e9a06", # class: 'sx'131String.Regex: "#4e9a06", # class: 'sr'132String.Single: "#4e9a06", # class: 's1'133String.Symbol: "#4e9a06", # class: 'ss'134135Generic: "#000000", # class: 'g'136Generic.Deleted: "#a40000", # class: 'gd'137Generic.Emph: "italic #000000", # class: 'ge'138Generic.Error: "#ef2929", # class: 'gr'139Generic.Heading: "bold #000080", # class: 'gh'140Generic.Inserted: "#00A000", # class: 'gi'141Generic.Output: "#888", # class: 'go'142Generic.Prompt: "#745334", # class: 'gp'143Generic.Strong: "bold #000000", # class: 'gs'144Generic.Subheading: "bold #800080", # class: 'gu'145Generic.Traceback: "bold #a40000", # class: 'gt'146}147148149