| 1 |
# -*- coding: utf-8 -*- |
| 2 |
"""Sphinx build configuration file.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import sys |
| 6 |
|
| 7 |
from sphinx.ext import apidoc |
| 8 |
|
| 9 |
from docutils import nodes |
| 10 |
from docutils import transforms |
| 11 |
|
| 12 |
# Change PYTHONPATH to include artifacts module and dependencies. |
| 13 |
sys.path.insert(0, os.path.abspath('..')) |
| 14 |
|
| 15 |
import artifacts # pylint: disable=wrong-import-position |
| 16 |
|
| 17 |
import utils.dependencies # pylint: disable=wrong-import-position |
| 18 |
|
| 19 |
|
| 20 |
# -- General configuration ------------------------------------------------ |
| 21 |
|
| 22 |
# If your documentation needs a minimal Sphinx version, state it here. |
| 23 |
needs_sphinx = '2.0.1' |
| 24 |
|
| 25 |
# Add any Sphinx extension module names here, as strings. They can be |
| 26 |
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom |
| 27 |
# ones. |
| 28 |
extensions = [ |
| 29 |
'recommonmark', |
| 30 |
'sphinx.ext.autodoc', |
| 31 |
'sphinx.ext.coverage', |
| 32 |
'sphinx.ext.doctest', |
| 33 |
'sphinx.ext.napoleon', |
| 34 |
'sphinx.ext.viewcode', |
| 35 |
'sphinx_markdown_tables', |
| 36 |
'sphinx_rtd_theme', |
| 37 |
] |
| 38 |
|
| 39 |
# We cannot install architecture dependent Python modules on readthedocs, |
| 40 |
# therefore we mock most imports. |
| 41 |
pip_installed_modules = set() |
| 42 |
|
| 43 |
dependency_helper = utils.dependencies.DependencyHelper( |
| 44 |
dependencies_file=os.path.join('..', 'dependencies.ini'), |
| 45 |
test_dependencies_file=os.path.join('..', 'test_dependencies.ini')) |
| 46 |
modules_to_mock = set(dependency_helper.dependencies.keys()) |
| 47 |
modules_to_mock = modules_to_mock.difference(pip_installed_modules) |
| 48 |
|
| 49 |
autodoc_mock_imports = sorted(modules_to_mock) |
| 50 |
|
| 51 |
# Options for the Sphinx Napoleon extension, which reads Google-style |
| 52 |
# docstrings. |
| 53 |
napoleon_google_docstring = True |
| 54 |
napoleon_numpy_docstring = False |
| 55 |
napoleon_include_private_with_doc = False |
| 56 |
napoleon_include_special_with_doc = True |
| 57 |
|
| 58 |
# General information about the project. |
| 59 |
# pylint: disable=redefined-builtin |
| 60 |
project = 'Digital Forensics Artifacts Repository' |
| 61 |
copyright = 'The Digital Forensics Artifacts Repository authors' |
| 62 |
version = artifacts.__version__ |
| 63 |
release = artifacts.__version__ |
| 64 |
|
| 65 |
# Add any paths that contain templates here, relative to this directory. |
| 66 |
templates_path = ['_templates'] |
| 67 |
|
| 68 |
# List of patterns, relative to source directory, that match files and |
| 69 |
# directories to ignore when looking for source files. |
| 70 |
exclude_patterns = ['_build'] |
| 71 |
|
| 72 |
# The master toctree document. |
| 73 |
master_doc = 'index' |
| 74 |
|
| 75 |
# The name of the Pygments (syntax highlighting) style to use. |
| 76 |
pygments_style = 'sphinx' |
| 77 |
|
| 78 |
|
| 79 |
# -- Options for HTML output ---------------------------------------------- |
| 80 |
|
| 81 |
# The theme to use for HTML and HTML Help pages. See the documentation for |
| 82 |
# a list of builtin themes. |
| 83 |
html_theme = 'sphinx_rtd_theme' |
| 84 |
|
| 85 |
# Output file base name for HTML help builder. |
| 86 |
htmlhelp_basename = 'artifactsdoc' |
| 87 |
|
| 88 |
|
| 89 |
# -- Options linkcheck ---------------------------------------------------- |
| 90 |
|
| 91 |
linkcheck_ignore = [ |
| 92 |
] |
| 93 |
|
| 94 |
|
| 95 |
# -- Code to rewrite links for readthedocs -------------------------------- |
| 96 |
|
| 97 |
# This function is a Sphinx core event callback, the format of which is detailed |
| 98 |
# here: https://www.sphinx-doc.org/en/master/extdev/appapi.html#events |
| 99 |
|
| 100 |
# pylint: disable=unused-argument |
| 101 |
def RunSphinxAPIDoc(app): |
| 102 |
"""Runs sphinx-apidoc to auto-generate documentation. |
| 103 |
|
| 104 |
Args: |
| 105 |
app (sphinx.application.Sphinx): Sphinx application. Required by the |
| 106 |
the Sphinx event callback API. |
| 107 |
""" |
| 108 |
current_directory = os.path.abspath(os.path.dirname(__file__)) |
| 109 |
module_path = os.path.join(current_directory, '..', 'artifacts') |
| 110 |
api_directory = os.path.join(current_directory, 'sources', 'api') |
| 111 |
apidoc.main(['-o', api_directory, module_path, '--force']) |
| 112 |
|
| 113 |
|
| 114 |
class MarkdownLinkFixer(transforms.Transform): |
| 115 |
"""Transform definition to parse .md references to internal pages.""" |
| 116 |
|
| 117 |
default_priority = 1000 |
| 118 |
|
| 119 |
_URI_PREFIXES = [] |
| 120 |
|
| 121 |
def _FixLinks(self, node): |
| 122 |
"""Corrects links to .md files not part of the documentation. |
| 123 |
|
| 124 |
Args: |
| 125 |
node (docutils.nodes.Node): docutils node. |
| 126 |
|
| 127 |
Returns: |
| 128 |
docutils.nodes.Node: docutils node, with correct URIs outside |
| 129 |
of Markdown pages outside the documentation. |
| 130 |
""" |
| 131 |
if isinstance(node, nodes.reference) and 'refuri' in node: |
| 132 |
reference_uri = node['refuri'] |
| 133 |
for uri_prefix in self._URI_PREFIXES: |
| 134 |
if (reference_uri.startswith(uri_prefix) and not ( |
| 135 |
reference_uri.endswith('.asciidoc') or |
| 136 |
reference_uri.endswith('.md'))): |
| 137 |
node['refuri'] = reference_uri + '.md' |
| 138 |
break |
| 139 |
|
| 140 |
return node |
| 141 |
|
| 142 |
def _Traverse(self, node): |
| 143 |
"""Traverses the document tree rooted at node. |
| 144 |
|
| 145 |
Args: |
| 146 |
node (docutils.nodes.Node): docutils node. |
| 147 |
""" |
| 148 |
self._FixLinks(node) |
| 149 |
|
| 150 |
for child_node in node.children: |
| 151 |
self._Traverse(child_node) |
| 152 |
|
| 153 |
# pylint: disable=arguments-differ |
| 154 |
def apply(self): |
| 155 |
"""Applies this transform on document tree.""" |
| 156 |
self._Traverse(self.document) |
| 157 |
|
| 158 |
|
| 159 |
# pylint: invalid-name |
| 160 |
def setup(app): |
| 161 |
"""Called at Sphinx initialization. |
| 162 |
|
| 163 |
Args: |
| 164 |
app (sphinx.application.Sphinx): Sphinx application. |
| 165 |
""" |
| 166 |
# Triggers sphinx-apidoc to generate API documentation. |
| 167 |
app.connect('builder-inited', RunSphinxAPIDoc) |
| 168 |
app.add_config_value( |
| 169 |
'recommonmark_config', {'enable_auto_toc_tree': True}, True) |
| 170 |
app.add_transform(MarkdownLinkFixer) |