Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Extends quote sphinx extension (#367)
Browse files Browse the repository at this point in the history
* Updates quote sphinx extension

* Extends sphinx quote extension.
  • Loading branch information
sdpython committed Apr 25, 2022
1 parent 99c115c commit 4964c32
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 2 deletions.
128 changes: 128 additions & 0 deletions _unittests/ut_sphinxext/test_quote_extension.py
Expand Up @@ -401,6 +401,134 @@ def test_quote_disc(self):
if ":author: auteur" not in rst:
raise Exception(rst)

def test_quote_ado(self):
from docutils import nodes as skip_

content = """
.. quote::
:author: auteur
:ado: ado titre
:lid: label1
:pages: 234
:year: 2018
:title1: true
this code should appear___
next
""".replace(" ", "")
if sys.version_info[0] >= 3:
content = content.replace('u"', '"')

tives = [("quote", QuoteNode, quote_node,
visit_quote_node, depart_quote_node)]

html = rst2html(content, # fLOG=fLOG,
writer="html", keep_warnings=True,
directives=tives, extlinks={'issue': ('http://%s', '_issue_')})

temp = get_temp_folder(__file__, "temp_quote_ado", clean=False)
with open(os.path.join(temp, "test_quote_ado.html"), "w", encoding="utf8") as f:
f.write(html)

t1 = "this code should appear"
if t1 not in html:
raise Exception(html)
if "auteur" not in html:
raise Exception(html)
if "ado titre" not in html:
raise Exception(html)
if "234" not in html:
raise Exception(html)

tives = [("quote", QuoteNode, quote_node,
visit_quote_node_rst, depart_quote_node_rst)]

rst = rst2html(content, # fLOG=fLOG,
writer="rst", keep_warnings=True,
directives=tives, extlinks={'issue': ('http://%s', '_issue_')})

with open(os.path.join(temp, "test_quote_ado.rst"), "w", encoding="utf8") as f:
f.write(rst)

t1 = "this code should appear"
if t1 not in rst:
raise Exception(rst)
if "auteur" not in rst:
raise Exception(rst)
if "ado titre" not in rst:
raise Exception(rst)
if "234" not in rst:
raise Exception(rst)
if ".. quote::" not in rst:
raise Exception(rst)
if ":author: auteur" not in rst:
raise Exception(rst)

def test_quote_child(self):
from docutils import nodes as skip_

content = """
.. quote::
:author: auteur
:child: child titre
:lid: label1
:pages: 234
:year: 2018
:title1: true
this code should appear___
next
""".replace(" ", "")
if sys.version_info[0] >= 3:
content = content.replace('u"', '"')

tives = [("quote", QuoteNode, quote_node,
visit_quote_node, depart_quote_node)]

html = rst2html(content, # fLOG=fLOG,
writer="html", keep_warnings=True,
directives=tives, extlinks={'issue': ('http://%s', '_issue_')})

temp = get_temp_folder(__file__, "temp_quote_child", clean=False)
with open(os.path.join(temp, "test_quote_child.html"), "w", encoding="utf8") as f:
f.write(html)

t1 = "this code should appear"
if t1 not in html:
raise Exception(html)
if "auteur" not in html:
raise Exception(html)
if "child titre" not in html:
raise Exception(html)
if "234" not in html:
raise Exception(html)

tives = [("quote", QuoteNode, quote_node,
visit_quote_node_rst, depart_quote_node_rst)]

rst = rst2html(content, # fLOG=fLOG,
writer="rst", keep_warnings=True,
directives=tives, extlinks={'issue': ('http://%s', '_issue_')})

with open(os.path.join(temp, "test_quote_child.rst"), "w", encoding="utf8") as f:
f.write(rst)

t1 = "this code should appear"
if t1 not in rst:
raise Exception(rst)
if "auteur" not in rst:
raise Exception(rst)
if "child titre" not in rst:
raise Exception(rst)
if "234" not in rst:
raise Exception(rst)
if ".. quote::" not in rst:
raise Exception(rst)
if ":author: auteur" not in rst:
raise Exception(rst)


if __name__ == "__main__":
unittest.main()
23 changes: 22 additions & 1 deletion src/pyquickhelper/sphinxext/sphinx_quote_extension.py
Expand Up @@ -27,7 +27,8 @@ class QuoteNode(BaseAdmonition):
It takes the following options:
* *author*
* *book* or *manga* or *film* or *show* or *disc* or *comic*
* *book* or *manga* or *film* or *show* or *disc* or
*comic* or *child* or *ado*
* *year*
* *pages*
* *tag*
Expand Down Expand Up @@ -62,6 +63,8 @@ class QuoteNode(BaseAdmonition):
'book': directives.unchanged,
'manga': directives.unchanged,
'disc': directives.unchanged,
'ado': directives.unchanged,
'child': directives.unchanged,
'comic': directives.unchanged,
'show': directives.unchanged,
'film': directives.unchanged,
Expand Down Expand Up @@ -112,6 +115,8 @@ def __(text):
book = __(self.options.get('book', "").strip())
manga = __(self.options.get('manga', "").strip())
comic = __(self.options.get('comic', "").strip())
ado = __(self.options.get('ado', "").strip())
child = __(self.options.get('child', "").strip())
disc = __(self.options.get('disc', "").strip())
film = __(self.options.get('film', "").strip())
show = __(self.options.get('show', "").strip())
Expand All @@ -135,6 +140,10 @@ def __(text):
tnl = [] # pragma: no cover

if title1:
if ado:
tnl.append("**{0}**".format(ado))
if child:
tnl.append("**{0}**".format(child))
if comic:
tnl.append("**{0}**".format(comic))
if disc:
Expand All @@ -152,6 +161,10 @@ def __(text):
else:
if author:
tnl.append("**{0}**, ".format(author))
if ado:
tnl.append("*{0}*".format(ado))
if child:
tnl.append("*{0}*".format(child))
if comic:
tnl.append("*{0}*".format(comic))
if disc:
Expand All @@ -168,6 +181,12 @@ def __(text):
if author:
indexes.append(author)
indexes.append(TITLES[language_code]['author'] + "; " + author)
if ado:
indexes.append(ado)
indexes.append(TITLES[language_code]['ado'] + "; " + ado)
if child:
indexes.append(child)
indexes.append(TITLES[language_code]['child'] + "; " + child)
if comic:
indexes.append(comic)
indexes.append(TITLES[language_code]['comic'] + "; " + comic)
Expand Down Expand Up @@ -225,6 +244,8 @@ def __(text):
node['manga'] = manga
node['disc'] = disc
node['comic'] = comic
node['ado'] = ado
node['child'] = child
node['film'] = film
node['show'] = show
node['index'] = index
Expand Down
6 changes: 5 additions & 1 deletion src/pyquickhelper/texthelper/texts_language.py
Expand Up @@ -6,6 +6,7 @@

TITLES = {
'en': {
'ado': 'teen',
'allblogs': 'All blog posts',
'author': 'author',
'blocref_node': '',
Expand All @@ -18,11 +19,12 @@
'by title:': 'By title :',
'catsmaonths': 'All categories and months',
'changes': 'Changes',
'child': 'child',
'cmdmes': '(<<%s>> : %s, line %d)',
'code': 'code',
'comic': 'comic',
'download': 'download',
'disc': 'disc',
'download': 'download',
'exmes': '(<<%s>> : %s, line %d)',
'exref_node': 'Examples',
'FAQ': 'FAQ',
Expand Down Expand Up @@ -59,6 +61,7 @@
'unhide': 'unhide',
},
'fr': {
'ado': 'ado',
'allblogs': 'Tous les articles de blog',
'author': 'auteur',
'blocref_node': '',
Expand All @@ -71,6 +74,7 @@
'by title:': 'Par titre :',
'catsmaonths': 'Catégories et mois',
'changes': 'Changements',
'child': 'enfant',
'cmdmes': '(<<%s>> : %s, line %d)',
'code': 'code',
'comic': 'bande dessinée',
Expand Down

0 comments on commit 4964c32

Please sign in to comment.