Skip to content

Commit

Permalink
Merge pull request #92 from vitorbrandao/GH-78-readthedocs-fix
Browse files Browse the repository at this point in the history
GH-78 Update readthedocs extensions
  • Loading branch information
vitorbrandao committed Nov 4, 2021
2 parents fb2d575 + e6f7213 commit 00bfe23
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 61 deletions.
56 changes: 42 additions & 14 deletions docs/_exts/sensio/sphinx/configurationblock.py
Expand Up @@ -6,29 +6,47 @@

from docutils.parsers.rst import Directive, directives
from docutils import nodes
from string import upper


class configurationblock(nodes.General, nodes.Element):
pass


class ConfigurationBlock(Directive):
has_content = True
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = True
option_spec = {}
formats = {
'html': 'HTML',
'xml': 'XML',
'php': 'PHP',
'yaml': 'YAML',
'jinja': 'Twig',
'html+jinja': 'Twig',
'jinja+html': 'Twig',
'php+html': 'PHP',
'html+php': 'PHP',
'ini': 'INI',
'html': 'HTML',
'xml': 'XML',
'php': 'PHP',
'yaml': 'YAML',
'jinja': 'Twig',
'html+jinja': 'Twig',
'jinja+html': 'Twig',
'twig': 'Twig',
'html+twig': 'Twig',
'twig+html': 'Twig',
'php+html': 'PHP',
'html+php': 'PHP',
'ini': 'INI',
'markdown': 'Markdown',
'php-annotations': 'Annotations',
'php-attributes': 'Attributes',
'php-standalone': 'Standalone Use',
'php-symfony': 'Framework Use',
'rst': 'reStructuredText',
'varnish2': 'Varnish 2',
'varnish3': 'Varnish 3',
'varnish4': 'Varnish 4',
'blackfire': 'Blackfire',
'bash': 'Bash',
'apache': 'Apache',
'nginx': 'Nginx',
'terminal': 'Terminal',
'env': '.env',
}

def run(self):
Expand All @@ -46,7 +64,8 @@ def run(self):
#targetnode = nodes.target('', '', ids=[targetid])
#targetnode.append(child)

innernode = nodes.emphasis(self.formats[child['language']], self.formats[child['language']])
innernode = nodes.emphasis(self.formats[child['language']],
self.formats[child['language']])

para = nodes.paragraph()
para += [innernode, child]
Expand All @@ -60,20 +79,29 @@ def run(self):

return [resultnode]


def visit_configurationblock_html(self, node):
self.body.append(self.starttag(node, 'div', CLASS='configuration-block'))


def depart_configurationblock_html(self, node):
self.body.append('</div>\n')


def visit_configurationblock_latex(self, node):
pass


def depart_configurationblock_latex(self, node):
pass


def setup(app):
app.add_node(configurationblock,
html=(visit_configurationblock_html, depart_configurationblock_html),
latex=(visit_configurationblock_latex, depart_configurationblock_latex))
html=(visit_configurationblock_html,
depart_configurationblock_html),
latex=(visit_configurationblock_latex,
depart_configurationblock_latex))
app.add_directive('configuration-block', ConfigurationBlock)

return {'parallel_read_safe': True}
21 changes: 12 additions & 9 deletions docs/_exts/sensio/sphinx/php.py
Expand Up @@ -13,9 +13,13 @@
from sphinx.util.nodes import make_refnode
from sphinx.util.docfields import Field, GroupedField, TypedField


def setup(app):
app.add_domain(PHPDomain)

return {'parallel_read_safe': True}


class PHPXRefRole(XRefRole):
def process_link(self, env, refnode, has_explicit_title, title, target):
# basically what sphinx.domains.python.PyXRefRole does
Expand All @@ -27,29 +31,28 @@ def process_link(self, env, refnode, has_explicit_title, title, target):
title = title[1:]
ns = title.rfind('\\')
if ns != -1:
title = title[ns+1:]
title = title[ns + 1:]
if target[0:1] == '\\':
target = target[1:]
refnode['refspecific'] = True
return title, target


class PHPDomain(Domain):
"""PHP language domain."""
name = 'php'
label = 'PHP'
# if you add a new object type make sure to edit JSObject.get_index_string
object_types = {
}
directives = {
}
object_types = {}
directives = {}
roles = {
'func': PHPXRefRole(fix_parens=True),
'func': PHPXRefRole(fix_parens=True),
'class': PHPXRefRole(),
'data': PHPXRefRole(),
'attr': PHPXRefRole(),
'data': PHPXRefRole(),
'attr': PHPXRefRole(),
}
initial_data = {
'objects': {}, # fullname -> docname, objtype
'objects': {}, # fullname -> docname, objtype
}

def clear_doc(self, docname):
Expand Down
156 changes: 127 additions & 29 deletions docs/_exts/sensio/sphinx/phpcode.py
Expand Up @@ -7,105 +7,203 @@
from docutils import nodes, utils

from sphinx.util.nodes import split_explicit_title
from string import lower

def php_namespace_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):

def php_namespace_role(typ,
rawtext,
text,
lineno,
inliner,
options={},
content=[]):
text = utils.unescape(text)
env = inliner.document.settings.env
base_url = env.app.config.api_url
has_explicit_title, title, namespace = split_explicit_title(text)

try:
full_url = base_url % namespace.replace('\\', '/') + '.html'
full_url = base_url % namespace.replace('\\', '/')
except (TypeError, ValueError):
env.warn(env.docname, 'unable to expand %s api_url with base '
'URL %r, please make sure the base contains \'%%s\' '
'exactly once' % (typ, base_url))
env.warn(
env.docname, 'unable to expand %s api_url with base '
'URL %r, please make sure the base contains \'%%s\' '
'exactly once' % (typ, base_url))
full_url = base_url + utils.escape(full_class)
if not has_explicit_title:
name = namespace.lstrip('\\')
ns = name.rfind('\\')
if ns != -1:
name = name[ns+1:]
name = name[ns + 1:]
title = name
list = [nodes.reference(title, title, internal=False, refuri=full_url, reftitle=namespace)]
list = [
nodes.reference(title,
title,
internal=False,
refuri=full_url,
reftitle=namespace)
]
pnode = nodes.literal('', '', *list)
return [pnode], []

def php_class_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):

def php_class_role(typ, rawtext, text, lineno, inliner, options={},
content=[]):
text = utils.unescape(text)
env = inliner.document.settings.env
base_url = env.app.config.api_url
has_explicit_title, title, full_class = split_explicit_title(text)

try:
full_url = base_url % full_class.replace('\\', '/') + '.html'
full_url = base_url % full_class.replace('\\', '/')
except (TypeError, ValueError):
env.warn(env.docname, 'unable to expand %s api_url with base '
'URL %r, please make sure the base contains \'%%s\' '
'exactly once' % (typ, base_url))
env.warn(
env.docname, 'unable to expand %s api_url with base '
'URL %r, please make sure the base contains \'%%s\' '
'exactly once' % (typ, base_url))
full_url = base_url + utils.escape(full_class)
if not has_explicit_title:
class_name = full_class.lstrip('\\')
ns = class_name.rfind('\\')
if ns != -1:
class_name = class_name[ns+1:]
class_name = class_name[ns + 1:]
title = class_name
list = [nodes.reference(title, title, internal=False, refuri=full_url, reftitle=full_class)]
list = [
nodes.reference(title,
title,
internal=False,
refuri=full_url,
reftitle=full_class)
]
pnode = nodes.literal('', '', *list)
return [pnode], []

def php_method_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):

def php_method_role(typ,
rawtext,
text,
lineno,
inliner,
options={},
content=[]):
text = utils.unescape(text)
env = inliner.document.settings.env
base_url = env.app.config.api_url
has_explicit_title, title, class_and_method = split_explicit_title(text)

ns = class_and_method.rfind('::')
full_class = class_and_method[:ns]
method = class_and_method[ns+2:]
method = class_and_method[ns + 2:]

try:
full_url = base_url % full_class.replace('\\', '/') + '.html' + '#method_' + method
full_url = base_url % full_class.replace('\\', '/')
except (TypeError, ValueError):
env.warn(env.docname, 'unable to expand %s api_url with base '
'URL %r, please make sure the base contains \'%%s\' '
'exactly once' % (typ, base_url))
env.warn(
env.docname, 'unable to expand %s api_url with base '
'URL %r, please make sure the base contains \'%%s\' '
'exactly once' % (typ, base_url))
full_url = base_url + utils.escape(full_class)
if not has_explicit_title:
title = method + '()'
list = [nodes.reference(title, title, internal=False, refuri=full_url, reftitle=full_class + '::' + method + '()')]
list = [
nodes.reference(title,
title,
internal=False,
refuri=full_url,
reftitle=full_class + '::' + method + '()')
]
pnode = nodes.literal('', '', *list)
return [pnode], []

def php_phpclass_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):

def php_phpclass_role(typ,
rawtext,
text,
lineno,
inliner,
options={},
content=[]):
text = utils.unescape(text)
has_explicit_title, title, full_class = split_explicit_title(text)

full_url = 'http://php.net/manual/en/class.%s.php' % lower(full_class)
full_url = 'https://www.php.net/manual/en/class.%s.php' % full_class.lower(
)

if not has_explicit_title:
title = full_class
list = [nodes.reference(title, title, internal=False, refuri=full_url, reftitle=full_class)]
list = [
nodes.reference(title,
title,
internal=False,
refuri=full_url,
reftitle=full_class)
]
pnode = nodes.literal('', '', *list)
return [pnode], []

def php_phpfunction_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):

def php_phpmethod_role(typ,
rawtext,
text,
lineno,
inliner,
options={},
content=[]):
text = utils.unescape(text)
has_explicit_title, title, class_and_method = split_explicit_title(text)

ns = class_and_method.rfind('::')
full_class = class_and_method[:ns]
method = class_and_method[ns + 2:]

full_url = 'https://www.php.net/manual/en/%s.%s.php' % (
full_class.lower(), method.lower())

if not has_explicit_title:
title = full_class + '::' + method + '()'
list = [
nodes.reference(title,
title,
internal=False,
refuri=full_url,
reftitle=full_class)
]
pnode = nodes.literal('', '', *list)
return [pnode], []


def php_phpfunction_role(typ,
rawtext,
text,
lineno,
inliner,
options={},
content=[]):
text = utils.unescape(text)
has_explicit_title, title, full_function = split_explicit_title(text)

full_url = 'http://php.net/manual/en/function.%s.php' % lower(full_function.replace('_', '-'))
full_url = 'https://www.php.net/manual/en/function.%s.php' % full_function.replace(
'_', '-').lower()

if not has_explicit_title:
title = full_function
list = [nodes.reference(title, title, internal=False, refuri=full_url, reftitle=full_function)]
list = [
nodes.reference(title,
title,
internal=False,
refuri=full_url,
reftitle=full_function)
]
pnode = nodes.literal('', '', *list)
return [pnode], []


def setup(app):
app.add_config_value('api_url', {}, 'env')
app.add_config_value('api_url', '', 'env')
app.add_role('namespace', php_namespace_role)
app.add_role('class', php_class_role)
app.add_role('method', php_method_role)
app.add_role('phpclass', php_phpclass_role)
app.add_role('phpmethod', php_phpmethod_role)
app.add_role('phpfunction', php_phpfunction_role)

return {'parallel_read_safe': True}

0 comments on commit 00bfe23

Please sign in to comment.