Skip to content

Commit

Permalink
add sphinx
Browse files Browse the repository at this point in the history
  • Loading branch information
asanchezyali committed Nov 23, 2023
1 parent 1826a56 commit 0c0fa55
Show file tree
Hide file tree
Showing 20 changed files with 538 additions and 123 deletions.
Empty file added .github/workflows/docs.yaml
Empty file.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<div align="center">
<a href="https://github.com/asanchezyali/Zmodn#readme">
<img src="logo/logo.drawio.svg" alt="Logo" width="100%" height="140">
<img src="logo/logo.svg" alt="Logo" width="100%" height="140">
</a>
<a href="https://github.com//Zmodn/actions/workflows/docs.yaml"><img src="https://github.com/mhostetter/galois/actions/workflows/docs.yaml/badge.svg"></a>
<a href="https://github.com//Zmodn/actions/workflows/lint.yaml"><img src="https://github.com/mhostetter/galois/actions/workflows/lint.yaml/badge.svg"></a>
Expand Down
2 changes: 2 additions & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
api/
build/
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
50 changes: 50 additions & 0 deletions docs/_static/extra.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Set the colors for the light theme */
[data-md-color-scheme="default"] {
--md-primary-fg-color: #344FA1;
--md-accent-fg-color: #4564C3;
}

/* Set the colors for the dark theme */
[data-md-color-scheme="slate"] {
--md-primary-fg-color: #EAA915;
--md-accent-fg-color: #EEBA44;
}
[data-md-color-scheme="slate"] .md-header {
background-color: #344FA1;
}
[data-md-color-scheme="slate"] .md-tabs {
background-color: #344FA1;
}

/* @media screen and (max-width: 76.1875em) */
.md-nav--primary .md-nav__title[for=__drawer] {
background-color: #344FA1;
}
.md-source {
background-color: #344FA1;
}

/* Fix line numbers in code blocks */
.linenos {
background-color: var(--md-default-bg-color--light);
margin-right: 0.5rem;
}

/* Override md-tab-set colors. */
.md-typeset .tabbed-labels {
--md-accent-fg-color: var(--md-primary-fg-color);
}

/* Modify Sphinx Immaterial python-apigen linkable parameter coloring. */
.sig-param a.reference span .pre {
color: var(--md-code-hl-generic-color);
}
.sig-param a.reference span .pre:hover {
color: var(--md-accent-fg-color);
}

/* Override announcement banner colors */
.md-banner {
background-color: var(--md-typeset-mark-color);
color: var(--md-default-fg-color);
}
17 changes: 17 additions & 0 deletions docs/_templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "!base.html" %}

{% block announce %}
<p>
Enjoying the library? Give us a
<span class="md-icon si-icon-inline octicons_star-16"></span>
on
<a class="reference external" href="https://github.com/asanchezyali/Zmodn">GitHub</a>.
</p>
{% endblock %}

{% block outdated %}
You're not viewing the latest version.
<a href="{{ '../' ~ base_url}}">
<strong>Click here to go to latest.</strong>
</a>
{% endblock %}
148 changes: 148 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import os
import sys

sys.path.insert(0, os.path.abspath("."))
sys.path.insert(0, os.path.abspath(".."))


# -- Project information -----------------------------------------------------

project = "Zmodn"
copyright = "2023, Alejandro Sánchez Yalí"
author = "Alejandro Sánchez Yalí"


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
"sphinx.ext.mathjax",
"sphinx.ext.intersphinx",
"sphinx_last_updated_by_git",
"sphinx_immaterial",
"sphinx_immaterial.apidoc.python.apigen",
"sphinx_math_dollar",
"myst_parser",
"sphinx_design",
"IPython.sphinxext.ipython_console_highlighting",
"IPython.sphinxext.ipython_directive",
"ipython_with_reprs",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = "sphinx_immaterial"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]


html_css_files = [
"extra.css",
]

# Define a custom inline Python syntax highlighting literal
rst_prolog = """
.. role:: python(code)
:language: python
:class: highlight
"""
# Sets the default role of `content` to :python:`content`, which uses the custom Python syntax highlighting inline literal
default_role = "python"

html_title = "Zmodn"
html_favicon = "../logo/favicon.png"
html_logo = "../logo/favicon.png"

# Sphinx Immaterial theme options
html_theme_options = {
"icon": {
"repo": "fontawesome/brands/github",
},
"repo_url": "https://github.com/asanchezyali/Zmodn",
"repo_name": "asanchezyali/Zmodn",
"social": [
{
"icon": "fontawesome/brands/github",
"link": "https://github.com/asanchezyali/Zmodn",
},
{
"icon": "fontawesome/brands/twitter",
"link": "https://twitter.com/asanchezyali",
},
],
"edit_uri": "",
"globaltoc_collapse": False,
"features": [
# "navigation.expand",
"navigation.tabs",
# "toc.integrate",
# "navigation.sections",
# "navigation.instant",
# "header.autohide",
"navigation.top",
"navigation.tracking",
"toc.follow",
"toc.sticky",
"content.tabs.link",
"announce.dismiss",
],
"palette": [
{
"media": "(prefers-color-scheme: light)",
"scheme": "default",
"toggle": {
"icon": "material/weather-night",
"name": "Switch to dark mode",
},
},
{
"media": "(prefers-color-scheme: dark)",
"scheme": "slate",
"toggle": {
"icon": "material/weather-sunny",
"name": "Switch to light mode",
},
},
],
"analytics": {
"provider": "google",
"property": "G-4FW9NCNFZH",
},
"version_dropdown": True,
"version_json": "../versions.json",
}

html_last_updated_fmt = ""
html_use_index = True
html_domain_indices = True
20 changes: 20 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.. Zmodn documentation master file, created by
sphinx-quickstart on Thu Nov 23 16:31:16 2023.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to Zmodn's documentation!
=================================

.. toctree::
:maxdepth: 2
:caption: Contents:



Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
96 changes: 96 additions & 0 deletions docs/ipython_with_reprs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
Sphinx extension that adds `ipython-with-element_reprs` directive.
"""
from typing import List

import docutils.nodes
import sphinx.addnodes
import sphinx.application
import sphinx.util.docutils
import sphinx.util.logging

logger = sphinx.util.logging.getLogger(__name__)

ELEMENT_REPR_TO_TITLE = {
"int": "Integer",
"poly": "Polynomial",
"power": "Power",
}


class IPythonWithReprsDirective(sphinx.util.docutils.SphinxDirective):
has_content = True
required_arguments = 1
optional_arguments = 1
option_spec = {
"name": str,
}

def run(self) -> List[docutils.nodes.Node]:
self.assert_has_content()

# Parse input parameters
element_reprs = self.arguments[0].split(",")
titles = [ELEMENT_REPR_TO_TITLE[element_repr] for element_repr in element_reprs]
field = self.options.get("name", "GF")

ws = " "
new_lines = [
".. md-tab-set::",
"",
]

for element_repr, title in zip(element_reprs, titles):
new_lines += [
f"{ws}.. md-tab-item:: {title}",
"",
f"{ws}{ws}.. ipython:: python",
"",
]

# Set the Galois field element representation
first_code_line = self.content[0]
if first_code_line.startswith(f"{field} = "):
assert "repr=" not in first_code_line
if element_repr == "int":
new_first_code_line = first_code_line
else:
items = first_code_line.rsplit(")", 1)
assert len(items) == 2
items.insert(1, f', repr="{element_repr}")')
new_first_code_line = "".join(items)
new_lines += [
f"{ws}{ws}{ws}{new_first_code_line}",
]
else:
new_lines += [
f"{ws}{ws}{ws}@suppress",
f'{ws}{ws}{ws}{field}.repr("{element_repr}")',
f"{ws}{ws}{ws}{first_code_line}",
]

# Add the raw python code
for code_line in self.content[1:]:
new_lines += [
f"{ws}{ws}{ws}{code_line}",
]

# Reset the element representation
new_lines += [
f"{ws}{ws}{ws}@suppress",
f"{ws}{ws}{ws}{field}.repr()",
"",
]

self.state_machine.insert_input(new_lines, self.state_machine.input_lines.source(0))

return []


def setup(app: sphinx.application.Sphinx):
app.add_directive("ipython-with-reprs", IPythonWithReprsDirective)

return {
"parallel_read_safe": True,
"parallel_write_safe": True,
}
Loading

0 comments on commit 0c0fa55

Please sign in to comment.