Commit 24f6b2a7 authored by Leonid Onokhov's avatar Leonid Onokhov

Docs: added cfg-field directive

And converted nix-local-build.rst to use it

[ci skip]
parent f9a43474
# -*- coding: utf-8 -*-
import re
from docutils import nodes
......@@ -25,11 +26,44 @@ def parse_deprecated(txt):
except ValueError:
return True
def parse_flag(env, sig, signode):
import re
names = []
for i, flag in enumerate(sig.split(',')):
flag = flag.strip()
sep = '='
parts = flag.split('=')
if len(parts) == 1:
sep=' '
parts = flag.split()
if len(parts) == 0: continue
name = parts[0]
names.append(name)
sig = sep + ' '.join(parts[1:])
sig = re.sub(ur'<([-a-zA-Z ]+)>', ur'⟨\1⟩', sig)
if i > 0:
signode += addnodes.desc_name(', ', ', ')
signode += addnodes.desc_name(name, name)
if len(sig) > 0:
signode += addnodes.desc_addname(sig, sig)
return names[0]
class Meta(object):
'''
Meta data associated with object
'''
def __init__(self, since=None, deprecated=None):
self.since = since
self.deprecated = deprecated
class CabalPackageSection(Directive):
"""
Directive marks a package.cabal section described next
and adds it to index. Can be referenced with pkg-directive.
and adds it to index. Can be referenced with pkg-section-name.
"""
has_content = False
......@@ -66,11 +100,6 @@ class CabalPackageSection(Directive):
return [inode, node]
class Meta(object):
def __init__(self, since=None, deprecated=None):
self.since = since
self.deprecated = deprecated
class CabalField(ObjectDescription):
option_spec = {
'noindex': directives.flag,
......@@ -97,7 +126,7 @@ class CabalField(ObjectDescription):
parts = sig.split(':',1)
name = parts[0]
signode += addnodes.desc_name(name, name)
signode += addnodes.desc_name(':', ':')
signode += addnodes.desc_name(': ', ': ')
if len(parts) > 1:
rest = parts[1].strip()
......@@ -107,7 +136,8 @@ class CabalField(ObjectDescription):
rendered = render_meta(meta)
if rendered != '':
signode += addnodes.desc_annotation(' ' + rendered, ' '+rendered)
signode += addnodes.desc_addname(' ', ' ')
signode += addnodes.desc_annotation(rendered, rendered)
return name
......@@ -161,6 +191,30 @@ class CabalPackageFieldXRef(XRefRole):
return title, target
class ConfigField(CabalField):
def handle_signature(self, sig, signode):
sig = sig.strip()
if sig.startswith('-'):
name = parse_flag(self, sig, signode)
else:
name = super(ConfigField, self).handle_signature(sig, signode)
return name
def get_index_entry(self, env, name):
if name.startswith('-'):
parts = ('cfg-flag', name)
indexentry = name + '; cabal project option'
else:
parts = (self.objtype, name)
indexentry = name + '; project.cabal field'
targetname = '-'.join(parts)
return indexentry, targetname
def get_env_key(self, env, name):
return name
def make_data_keys(typ, target, node):
if typ == 'pkg-field':
section = node.get('cabal:section')
......@@ -171,19 +225,19 @@ def make_data_keys(typ, target, node):
def render_meta(meta):
if meta.deprecated is not None:
if isinstance(meta.deprecated, StrictVersion):
return ' (deprecated since:'+str(meta.deprecated) + ')'
return '(deprecated since:'+str(meta.deprecated) + ')'
else:
return '(deprecated)'
elif meta.since is not None:
return ' (since version: ' + str(meta.since) + ')'
return '(since version: ' + str(meta.since) + ')'
else:
return ''
def make_title(typ, key, meta):
if typ == 'pkg-section':
return "package.cabal " + key + "section" + render_meta(meta)
return "package.cabal " + key + " section " + render_meta(meta)
if typ == 'pkg-field':
elif typ == 'pkg-field':
section, name = key
if section is not None:
base = "package.cabal " + section + " section " + name + " field"
......@@ -192,45 +246,60 @@ def make_title(typ, key, meta):
return base + render_meta(meta)
elif typ == 'cfg-field':
return "project.cabal " + key + " field " + render_meta(meta)
def make_full_name(typ, key, meta):
if typ == 'pkg-section':
return 'pkg-section-' + key
if typ == 'pkg-field':
elif typ == 'pkg-field':
section, name = key
if section is not None:
return '-'.join(('pkg-field',section, name))
else:
return 'pkg-field-' + name
elif typ == 'cfg-field':
return 'cfg-field-' + key
else:
raise ValueError('Unknown object type: ' + typ)
class CabalDomain(Domain):
name = 'cabal'
label = 'Cabal'
object_types = {
'pkg-section': ObjType(l_('pkg-section'), 'pkg-section'),
'pkg-field' : ObjType(l_('pkg-field') , 'pkg-field' ),
'cfg-field' : ObjType(l_('cfg-field') , 'cfg-field' ),
}
directives = {
'pkg-section': CabalPackageSection,
'pkg-field' : CabalPackageField
'pkg-field' : CabalPackageField,
'cfg-field' : ConfigField,
}
roles = {
'pkg-section': XRefRole(warn_dangling=True),
'pkg-field' : CabalPackageFieldXRef(warn_dangling=True),
'cfg-field' : XRefRole(warn_dangling=True),
'cfg-flag' : XRefRole(warn_dangling=True),
}
initial_data = {
'pkg-sections': {},
'pkg-fields': {},
'pkg-fields' : {},
'cfg-fields' : {},
}
indices = [
]
types = {
'pkg-section': 'pkg-sections',
'pkg-field' : 'pkg-fields'
'pkg-field' : 'pkg-fields',
'cfg-field' : 'cfg-fields',
}
def clear_doc(self, docname):
for k in ['pkg-sections', 'pkg-fields']:
for k in ['pkg-sections', 'pkg-fields', 'cfg-fields']:
for name, (fn, _, _) in self.data[k].items():
if fn == docname:
del self.data[k][comname]
......@@ -248,7 +317,7 @@ class CabalDomain(Domain):
return make_refnode(builder, fromdocname, doc, ref, contnode, title)
def get_objects(self):
for typ in ['pkg-section', 'pkg-field']:
for typ in ['pkg-section', 'pkg-field', 'cfg-field']:
key = self.types[typ]
for name, (fn, target, meta) in self.data[key].items():
title = make_title(typ, name, meta)
......
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment