| 1 |
# |
| 2 |
# Pygments documentation build configuration file |
| 3 |
# |
| 4 |
|
| 5 |
import os |
| 6 |
import re |
| 7 |
import sys |
| 8 |
|
| 9 |
# If extensions (or modules to document with autodoc) are in another directory, |
| 10 |
# add these directories to sys.path here. If the directory is relative to the |
| 11 |
# documentation root, use os.path.abspath to make it absolute, like shown here. |
| 12 |
sys.path.insert(0, os.path.abspath('..')) |
| 13 |
|
| 14 |
# ruff: noqa: E402 |
| 15 |
import pygments |
| 16 |
import pygments.formatters |
| 17 |
import pygments.lexers |
| 18 |
import pygments.styles |
| 19 |
import tests.contrast.test_contrasts as test_contrasts |
| 20 |
|
| 21 |
# -- General configuration ----------------------------------------------------- |
| 22 |
|
| 23 |
# If your documentation needs a minimal Sphinx version, state it here. |
| 24 |
#needs_sphinx = '1.0' |
| 25 |
|
| 26 |
# Add any Sphinx extension module names here, as strings. They can be extensions |
| 27 |
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. |
| 28 |
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'pygments.sphinxext'] |
| 29 |
|
| 30 |
# Add any paths that contain templates here, relative to this directory. |
| 31 |
templates_path = ['_templates'] |
| 32 |
|
| 33 |
# The suffix of source filenames. |
| 34 |
source_suffix = '.rst' |
| 35 |
|
| 36 |
# The encoding of source files. |
| 37 |
#source_encoding = 'utf-8-sig' |
| 38 |
|
| 39 |
# The master toctree document. |
| 40 |
master_doc = 'index' |
| 41 |
|
| 42 |
# General information about the project. |
| 43 |
project = 'Pygments' |
| 44 |
copyright = '2006-2024, Georg Brandl and Pygments contributors' |
| 45 |
|
| 46 |
# The version info for the project you're documenting, acts as replacement for |
| 47 |
# |version| and |release|, also used in various other places throughout the |
| 48 |
# built documents. |
| 49 |
# |
| 50 |
# The short X.Y version. |
| 51 |
version = pygments.__version__ |
| 52 |
# The full version, including alpha/beta/rc tags. |
| 53 |
release = version |
| 54 |
|
| 55 |
# The language for content autogenerated by Sphinx. Refer to documentation |
| 56 |
# for a list of supported languages. |
| 57 |
#language = None |
| 58 |
|
| 59 |
# There are two options for replacing |today|: either, you set today to some |
| 60 |
# non-false value, then it is used: |
| 61 |
#today = '' |
| 62 |
# Else, today_fmt is used as the format for a strftime call. |
| 63 |
#today_fmt = '%B %d, %Y' |
| 64 |
|
| 65 |
# List of patterns, relative to source directory, that match files and |
| 66 |
# directories to ignore when looking for source files. |
| 67 |
exclude_patterns = ['_build'] |
| 68 |
|
| 69 |
# The reST default role (used for this markup: `text`) to use for all documents. |
| 70 |
#default_role = None |
| 71 |
|
| 72 |
# If true, '()' will be appended to :func: etc. cross-reference text. |
| 73 |
#add_function_parentheses = True |
| 74 |
|
| 75 |
# If true, the current module name will be prepended to all description |
| 76 |
# unit titles (such as .. function::). |
| 77 |
#add_module_names = True |
| 78 |
|
| 79 |
# If true, sectionauthor and moduleauthor directives will be shown in the |
| 80 |
# output. They are ignored by default. |
| 81 |
#show_authors = False |
| 82 |
|
| 83 |
# The name of the Pygments (syntax highlighting) style to use. |
| 84 |
#pygments_style = 'sphinx' |
| 85 |
|
| 86 |
# A list of ignored prefixes for module index sorting. |
| 87 |
#modindex_common_prefix = [] |
| 88 |
|
| 89 |
|
| 90 |
# -- Options for HTML output --------------------------------------------------- |
| 91 |
|
| 92 |
# The theme to use for HTML and HTML Help pages. See the documentation for |
| 93 |
# a list of builtin themes. |
| 94 |
html_theme = 'pygments14' |
| 95 |
|
| 96 |
# Theme options are theme-specific and customize the look and feel of a theme |
| 97 |
# further. For a list of options available for each theme, see the |
| 98 |
# documentation. |
| 99 |
#html_theme_options = {} |
| 100 |
|
| 101 |
# Add any paths that contain custom themes here, relative to this directory. |
| 102 |
html_theme_path = ['_themes'] |
| 103 |
|
| 104 |
# The name for this set of Sphinx documents. If None, it defaults to |
| 105 |
# "<project> v<release> documentation". |
| 106 |
html_title = 'Pygments' |
| 107 |
|
| 108 |
# A shorter title for the navigation bar. Default is the same as html_title. |
| 109 |
#html_short_title = None |
| 110 |
|
| 111 |
# The name of an image file (relative to this directory) to place at the top |
| 112 |
# of the sidebar. |
| 113 |
#html_logo = None |
| 114 |
|
| 115 |
# The name of an image file (within the static path) to use as favicon of the |
| 116 |
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 |
| 117 |
# pixels large. |
| 118 |
html_favicon = '_static/favicon.ico' |
| 119 |
|
| 120 |
# Add any paths that contain custom static files (such as style sheets) here, |
| 121 |
# relative to this directory. They are copied after the builtin static files, |
| 122 |
# so a file named "default.css" will overwrite the builtin "default.css". |
| 123 |
html_static_path = ['_static'] |
| 124 |
|
| 125 |
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, |
| 126 |
# using the given strftime format. |
| 127 |
#html_last_updated_fmt = '%b %d, %Y' |
| 128 |
|
| 129 |
# If true, SmartyPants will be used to convert quotes and dashes to |
| 130 |
# typographically correct entities. |
| 131 |
#html_use_smartypants = True |
| 132 |
|
| 133 |
# Custom sidebar templates, maps document names to template names. |
| 134 |
html_sidebars = {'index': ['indexsidebar.html', 'searchbox.html']} |
| 135 |
|
| 136 |
# Additional templates that should be rendered to pages, maps page names to |
| 137 |
# template names. |
| 138 |
html_additional_pages = { |
| 139 |
'styles': 'styles.html', |
| 140 |
} |
| 141 |
|
| 142 |
if os.environ.get('WEBSITE_BUILD'): |
| 143 |
html_additional_pages['demo'] = 'demo.html' |
| 144 |
html_static_path.append('_build/pyodide') |
| 145 |
|
| 146 |
# If false, no module index is generated. |
| 147 |
#html_domain_indices = True |
| 148 |
|
| 149 |
# If false, no index is generated. |
| 150 |
#html_use_index = True |
| 151 |
|
| 152 |
# If true, the index is split into individual pages for each letter. |
| 153 |
#html_split_index = False |
| 154 |
|
| 155 |
# If true, links to the reST sources are added to the pages. |
| 156 |
#html_show_sourcelink = True |
| 157 |
|
| 158 |
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. |
| 159 |
#html_show_sphinx = True |
| 160 |
|
| 161 |
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. |
| 162 |
#html_show_copyright = True |
| 163 |
|
| 164 |
# If true, an OpenSearch description file will be output, and all pages will |
| 165 |
# contain a <link> tag referring to it. The value of this option must be the |
| 166 |
# base URL from which the finished HTML is served. |
| 167 |
#html_use_opensearch = '' |
| 168 |
|
| 169 |
# This is the file name suffix for HTML files (e.g. ".xhtml"). |
| 170 |
#html_file_suffix = None |
| 171 |
|
| 172 |
# Output file base name for HTML help builder. |
| 173 |
htmlhelp_basename = 'Pygments' |
| 174 |
|
| 175 |
|
| 176 |
# -- Options for LaTeX output -------------------------------------------------- |
| 177 |
|
| 178 |
latex_elements = { |
| 179 |
# The paper size ('letterpaper' or 'a4paper'). |
| 180 |
#'papersize': 'letterpaper', |
| 181 |
|
| 182 |
# The font size ('10pt', '11pt' or '12pt'). |
| 183 |
#'pointsize': '10pt', |
| 184 |
|
| 185 |
# Additional stuff for the LaTeX preamble. |
| 186 |
#'preamble': '', |
| 187 |
} |
| 188 |
|
| 189 |
# Grouping the document tree into LaTeX files. List of tuples |
| 190 |
# (source start file, target name, title, author, documentclass [howto/manual]). |
| 191 |
latex_documents = [ |
| 192 |
('docs/index', 'Pygments.tex', 'Pygments Documentation', |
| 193 |
'Pygments authors', 'manual'), |
| 194 |
] |
| 195 |
|
| 196 |
# The name of an image file (relative to this directory) to place at the top of |
| 197 |
# the title page. |
| 198 |
#latex_logo = None |
| 199 |
|
| 200 |
# For "manual" documents, if this is true, then toplevel headings are parts, |
| 201 |
# not chapters. |
| 202 |
#latex_use_parts = False |
| 203 |
|
| 204 |
# If true, show page references after internal links. |
| 205 |
#latex_show_pagerefs = False |
| 206 |
|
| 207 |
# If true, show URL addresses after external links. |
| 208 |
#latex_show_urls = False |
| 209 |
|
| 210 |
# Documents to append as an appendix to all manuals. |
| 211 |
#latex_appendices = [] |
| 212 |
|
| 213 |
# If false, no module index is generated. |
| 214 |
#latex_domain_indices = True |
| 215 |
|
| 216 |
|
| 217 |
# -- Options for manual page output -------------------------------------------- |
| 218 |
|
| 219 |
# One entry per manual page. List of tuples |
| 220 |
# (source start file, name, description, authors, manual section). |
| 221 |
man_pages = [ |
| 222 |
('docs/index', 'pygments', 'Pygments Documentation', |
| 223 |
['Pygments authors'], 1) |
| 224 |
] |
| 225 |
|
| 226 |
# If true, show URL addresses after external links. |
| 227 |
#man_show_urls = False |
| 228 |
|
| 229 |
|
| 230 |
# Example configuration for intersphinx: refer to the Python standard library. |
| 231 |
#intersphinx_mapping = {'http://docs.python.org/': None} |
| 232 |
|
| 233 |
rst_prolog = f'.. |language_count| replace:: {len(list(pygments.lexers.get_all_lexers()))}' |
| 234 |
|
| 235 |
def pg_context(app, pagename, templatename, ctx, event_arg): |
| 236 |
# casting string to bool doesn't work, we'll use 0 to disable |
| 237 |
ctx['demo_active'] = os.environ.get('WEBSITE_BUILD') != '0' |
| 238 |
|
| 239 |
if pagename == 'demo': |
| 240 |
ctx['lexers'] = sorted(pygments.lexers.get_all_lexers(plugins=False), key=lambda x: x[0].lower()) |
| 241 |
|
| 242 |
if pagename in ('styles', 'demo'): |
| 243 |
with open('examples/example.py', encoding='utf-8') as f: |
| 244 |
html = f.read() |
| 245 |
lexer = pygments.lexers.get_lexer_for_filename('example.py') |
| 246 |
min_contrasts = test_contrasts.min_contrasts() |
| 247 |
ctx['styles_aa'] = [] |
| 248 |
ctx['styles_sub_aa'] = [] |
| 249 |
# Use STYLE_MAP directly so we don't get plugins as with get_all_styles(). |
| 250 |
for style in pygments.styles.STYLE_MAP: |
| 251 |
if not pygments.styles.get_style_by_name(style).web_style_gallery_exclude: |
| 252 |
aa = min_contrasts[style] >= test_contrasts.WCAG_AA_CONTRAST |
| 253 |
bg_r, bg_g, bg_b = test_contrasts.hex2rgb(pygments.styles.get_style_by_name(style).background_color) |
| 254 |
ctx['styles_aa' if aa else 'styles_sub_aa'].append( |
| 255 |
dict( |
| 256 |
name=style, |
| 257 |
html=pygments.highlight( |
| 258 |
html, |
| 259 |
lexer, |
| 260 |
pygments.formatters.HtmlFormatter(noclasses=True, style=style), |
| 261 |
), |
| 262 |
# from https://en.wikipedia.org/wiki/Relative_luminance |
| 263 |
bg_luminance=(0.2126*bg_r + 0.7152*bg_g + 0.0722*bg_b) |
| 264 |
) |
| 265 |
) |
| 266 |
|
| 267 |
# sort styles according to their background luminance (light styles first) |
| 268 |
# if styles have the same background luminance sort them by their name |
| 269 |
def sortkey(s): |
| 270 |
return (-s['bg_luminance'], s['name']) |
| 271 |
# the default style is always displayed first |
| 272 |
default_style = ctx['styles_aa'].pop(0) |
| 273 |
ctx['styles_aa'].sort(key=sortkey) |
| 274 |
ctx['styles_aa'].insert(0, default_style) |
| 275 |
ctx['styles_sub_aa'].sort(key=sortkey) |
| 276 |
|
| 277 |
|
| 278 |
def source_read(app, docname, source): |
| 279 |
# linkify issue / PR numbers in changelog |
| 280 |
if docname == 'docs/changelog': |
| 281 |
with open('../CHANGES', encoding='utf-8') as f: |
| 282 |
changelog = f.read() |
| 283 |
|
| 284 |
idx = changelog.find('\nVersion 2.4.2\n') |
| 285 |
|
| 286 |
def linkify(match): |
| 287 |
url = 'https://github.com/pygments/pygments/issues/' + match[1] |
| 288 |
return f'`{match[0]} <{url}>`_' |
| 289 |
|
| 290 |
linkified = re.sub(r'(?:PR)?#([0-9]+)\b', linkify, changelog[:idx]) |
| 291 |
source[0] = linkified + changelog[idx:] |
| 292 |
|
| 293 |
|
| 294 |
def setup(app): |
| 295 |
app.connect('html-page-context', pg_context) |
| 296 |
app.connect('source-read', source_read) |