* [gentoo-commits] proj/policy-guide:master commit in: exts/
@ 2020-02-25 15:16 Michał Górny
0 siblings, 0 replies; 3+ messages in thread
From: Michał Górny @ 2020-02-25 15:16 UTC (permalink / raw
To: gentoo-commits
commit: 59a72da360e2eff53ef510261c3f6307148b52dc
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Sat Feb 22 17:15:51 2020 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Tue Feb 25 15:15:23 2020 +0000
URL: https://gitweb.gentoo.org/proj/policy-guide.git/commit/?id=59a72da3
Generate a Policy Index
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>
exts/policyident.py | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 75 insertions(+), 3 deletions(-)
diff --git a/exts/policyident.py b/exts/policyident.py
index 6093c8c..7ecc9f2 100644
--- a/exts/policyident.py
+++ b/exts/policyident.py
@@ -2,13 +2,44 @@
# (c) 2020 Michał Górny
# 2-clause BSD license
+import collections
+
from docutils import nodes
+from sphinx.domains import Index
from sphinx.util import logging
logger = logging.getLogger(__name__)
+Policy = collections.namedtuple('Policy', ('id', 'title', 'docname',
+ 'chapter'))
+
+
+class PolicyIndex(Index):
+ name = 'policy-index'
+ localname = 'Policy Index'
+ shortname = 'Policy Index'
+
+ def generate(self, docnames=None):
+ env = self.domain.env
+ if not hasattr(env, 'policy_index'):
+ env.policy_index = []
+
+ entries = collections.defaultdict(list)
+ for p in env.policy_index:
+ if docnames is not None and p.docname not in docnames:
+ continue
+ entries[p.chapter].append(('PG' + p.id, # name
+ 0, # subtype
+ p.docname, # docname
+ 'pg' + p.id, # anchor
+ p.title, # extra
+ '', # qualifier
+ '')) # descr
+
+ return ([(k, sorted(v)) for k, v in entries.items()], False)
+
def find_pg_id(section):
# first child should be title
@@ -17,7 +48,7 @@ def find_pg_id(section):
# second child should be field list
cl = section.children[1]
if not isinstance(cl, nodes.field_list):
- return None
+ return None, title.astext(), None
for f in cl.traverse(nodes.field):
fn = next(iter(f.traverse(nodes.field_name)))
@@ -29,20 +60,61 @@ def find_pg_id(section):
if fv.astext() != iv:
raise RuntimeError('PG value must be 4 digits, zero-padded ({})'
.format(iv))
- return iv
+
+ el = section
+ titles = []
+ while el.parent is not None:
+ title = el.children[0]
+ assert isinstance(title, nodes.title)
+ titles.append(title.astext())
+ el = el.parent
+ # combine all section titles up to but excluding
+ # the chapter title
+ title = ': '.join(reversed(titles[:-1]))
+
+ return iv, title, titles[-1]
logger.warning('%s: no PG identifier found', title.astext())
+ return None, title.astext(), None
def on_doctree_read(app, doctree):
+ env = app.builder.env
+ if not hasattr(env, 'policy_index'):
+ env.policy_index = []
+
for node in doctree.traverse(nodes.section):
- pg_id = find_pg_id(node)
+ pg_id, title, chapter = find_pg_id(node)
if pg_id is not None:
node['ids'].insert(0, 'pg' + pg_id)
+ env.policy_index.append(Policy(pg_id, title, env.docname,
+ chapter))
+
+
+def on_env_purge_doc(app, env, docname):
+ if not hasattr(env, 'policy_index'):
+ return
+
+ env.policy_index = [p for p in env.policy_index
+ if p.docname != docname]
+
+
+def on_env_merge_info(app, env, docnames, other):
+ if not hasattr(other, 'policy_index'):
+ return
+ if not hasattr(env, 'policy_index'):
+ env.policy_index = []
+
+ env.policy_index.extend(other.policy_index)
def setup(app):
app.connect('doctree-read', on_doctree_read)
+ app.connect('env-purge-doc', on_env_purge_doc)
+ app.connect('env-merge-info', on_env_merge_info)
+ app.add_index_to_domain('std', PolicyIndex)
return {
'version': '0',
+ 'parallel_read_safe': True,
+ 'parallel_write_safe': True,
}
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [gentoo-commits] proj/policy-guide:master commit in: exts/
@ 2020-02-25 15:16 Michał Górny
0 siblings, 0 replies; 3+ messages in thread
From: Michał Górny @ 2020-02-25 15:16 UTC (permalink / raw
To: gentoo-commits
commit: a7f75d8b7bb207469b83d602600487f36346dc8b
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Mon Feb 24 06:56:38 2020 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Tue Feb 25 15:15:25 2020 +0000
URL: https://gitweb.gentoo.org/proj/policy-guide.git/commit/?id=a7f75d8b
Use #pg links in TOC
Thanks for arzano for the solution.
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>
Closes: https://github.com/gentoo/policy-guide/pull/14
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>
exts/policyident.py | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/exts/policyident.py b/exts/policyident.py
index 7ecc9f2..8cdc636 100644
--- a/exts/policyident.py
+++ b/exts/policyident.py
@@ -7,6 +7,7 @@ import collections
from docutils import nodes
from sphinx.domains import Index
+from sphinx.environment.collectors.toctree import TocTreeCollector
from sphinx.util import logging
@@ -15,6 +16,8 @@ logger = logging.getLogger(__name__)
Policy = collections.namedtuple('Policy', ('id', 'title', 'docname',
'chapter'))
+toccollector = TocTreeCollector()
+
class PolicyIndex(Index):
name = 'policy-index'
@@ -90,6 +93,9 @@ def on_doctree_read(app, doctree):
env.policy_index.append(Policy(pg_id, title, env.docname,
chapter))
+ # update the table of conents to use the 'pgXXXX' ids
+ toccollector.process_doc(app, doctree)
+
def on_env_purge_doc(app, env, docname):
if not hasattr(env, 'policy_index'):
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [gentoo-commits] proj/policy-guide:master commit in: exts/
@ 2020-03-02 5:22 Michał Górny
0 siblings, 0 replies; 3+ messages in thread
From: Michał Górny @ 2020-03-02 5:22 UTC (permalink / raw
To: gentoo-commits
commit: eb346d01bba1e507085f4e338e1902a7f99d4305
Author: Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Wed Feb 26 13:37:14 2020 +0000
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Mon Mar 2 05:15:25 2020 +0000
URL: https://gitweb.gentoo.org/proj/policy-guide.git/commit/?id=eb346d01
Sort Policy Index keys by PGxx identifiers
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>
Closes: https://github.com/gentoo/policy-guide/pull/15
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>
exts/policyident.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/exts/policyident.py b/exts/policyident.py
index 8cdc636..9679bba 100644
--- a/exts/policyident.py
+++ b/exts/policyident.py
@@ -41,7 +41,9 @@ class PolicyIndex(Index):
'', # qualifier
'')) # descr
- return ([(k, sorted(v)) for k, v in entries.items()], False)
+ return (sorted([(k, sorted(v)) for k, v in entries.items()],
+ key=lambda kv: kv[1]),
+ False)
def find_pg_id(section):
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-03-02 5:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-02-25 15:16 [gentoo-commits] proj/policy-guide:master commit in: exts/ Michał Górny
-- strict thread matches above, loose matches on Subject: below --
2020-02-25 15:16 Michał Górny
2020-03-02 5:22 Michał Górny
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox