Skip to content

Commit

Permalink
Add support for include, glossary and csv-table directives (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Feb 26, 2020
1 parent dfaa70e commit 46bf7e1
Show file tree
Hide file tree
Showing 21 changed files with 777 additions and 321 deletions.
6 changes: 3 additions & 3 deletions myst_parser/block_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def read(cls, lines):
class Document(block_token.BlockToken):
"""Document token."""

def __init__(self, lines):
def __init__(self, lines, start_line=0, inc_front_matter=True):

self.footnotes = {}
block_token._root_node = self
Expand All @@ -89,11 +89,11 @@ def __init__(self, lines):
if isinstance(lines, str):
lines = lines.splitlines(keepends=True)
lines = [line if line.endswith("\n") else "{}\n".format(line) for line in lines]
start_line = 0
self.children = []
if lines and lines[0].startswith("---"):
front_matter = FrontMatter(lines)
self.children.append(front_matter)
if inc_front_matter:
self.children.append(front_matter)
start_line = front_matter.range[1]
lines = lines[start_line:]
self.children.extend(tokenize(lines, start_line))
Expand Down
295 changes: 251 additions & 44 deletions myst_parser/docutils_renderer.py

Large diffs are not rendered by default.

157 changes: 126 additions & 31 deletions tests/test_renderers/sphinx_directives.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,29 @@
{
"name": "admonition",
"class": "docutils.parsers.rst.directives.admonitions.Admonition",
"args": ["myclass"],
"args": [
"myclass"
],
"options": {},
"content": "a",
"output": "<admonition classes=\"admonition-myclass\">\n <title>\n myclass\n <paragraph>\n a"
},
{
"name": "sidebar",
"class": "docutils.parsers.rst.directives.body.Sidebar",
"args": ["sidebar title"],
"args": [
"sidebar title"
],
"options": {},
"content": "a",
"output": "<sidebar>\n <title>\n sidebar title\n <paragraph>\n a"
},
{
"name": "topic",
"class": "docutils.parsers.rst.directives.body.Topic",
"args": ["Topic Title"],
"args": [
"Topic Title"
],
"options": {},
"content": "a",
"output": "<topic>\n <title>\n Topic Title\n <paragraph>\n a"
Expand All @@ -113,7 +119,9 @@
{
"name": "rubric",
"class": "docutils.parsers.rst.directives.body.Rubric",
"args": ["Rubric Title"],
"args": [
"Rubric Title"
],
"options": {},
"output": "<rubric>\n Rubric Title"
},
Expand Down Expand Up @@ -161,7 +169,9 @@
{
"name": "image",
"class": "docutils.parsers.rst.directives.images.Image",
"args": ["path/to/image"],
"args": [
"path/to/image"
],
"options": {},
"output": "<image uri=\"path/to/image\">"
},
Expand All @@ -182,7 +192,9 @@
{
"name": "raw",
"class": "docutils.parsers.rst.directives.misc.Raw",
"args": ["raw"],
"args": [
"raw"
],
"options": {},
"content": "a",
"output": "<raw format=\"raw\" xml:space=\"preserve\">\n a"
Expand All @@ -206,7 +218,9 @@
{
"name": "class",
"class": "docutils.parsers.rst.directives.misc.Class",
"args": ["myclass"],
"args": [
"myclass"
],
"options": {},
"content": "a",
"output": "<paragraph classes=\"myclass\">\n a"
Expand All @@ -222,10 +236,12 @@
{
"name": "title",
"class": "docutils.parsers.rst.directives.misc.Title",
"args": ["title"],
"args": [
"title"
],
"options": {},
"output": "",
"doc_tag": "<document source=\"\" title=\"title\">"
"doc_tag": "<document source=\"notset\" title=\"title\">"
},
{
"name": "date",
Expand All @@ -240,7 +256,7 @@
"class": "docutils.parsers.rst.directives.misc.TestDirective",
"args": [],
"options": {},
"output": "<system_message level=\"1\" line=\"0\" source=\"\" type=\"INFO\">\n <paragraph>\n Directive processed. Type=\"restructuredtext-test-directive\", arguments=[], options={}, content: None"
"output": "<system_message level=\"1\" line=\"0\" source=\"notset\" type=\"INFO\">\n <paragraph>\n Directive processed. Type=\"restructuredtext-test-directive\", arguments=[], options={}, content: None"
},
{
"name": "contents",
Expand Down Expand Up @@ -289,28 +305,36 @@
{
"name": "default-domain",
"class": "sphinx.directives.DefaultDomain",
"args": ["mydomain"],
"args": [
"mydomain"
],
"options": {},
"output": ""
},
{
"name": "describe",
"class": "sphinx.directives.ObjectDescription",
"args": ["something"],
"args": [
"something"
],
"options": {},
"output": "<index entries=\"\">\n<desc desctype=\"describe\" domain=\"\" noindex=\"False\" objtype=\"describe\">\n <desc_signature first=\"False\">\n <desc_name xml:space=\"preserve\">\n something\n <desc_content>"
},
{
"name": "object",
"class": "sphinx.directives.ObjectDescription",
"args": ["something"],
"args": [
"something"
],
"options": {},
"output": "<index entries=\"\">\n<desc desctype=\"object\" domain=\"\" noindex=\"False\" objtype=\"object\">\n <desc_signature first=\"False\">\n <desc_name xml:space=\"preserve\">\n something\n <desc_content>"
},
{
"name": "highlight",
"class": "sphinx.directives.code.Highlight",
"args": ["something"],
"args": [
"something"
],
"options": {},
"output": "<highlightlang force=\"False\" lang=\"something\" linenothreshold=\"9223372036854775807\">"
},
Expand All @@ -332,9 +356,11 @@
{
"name": "literalinclude",
"class": "sphinx.directives.code.LiteralInclude",
"args": ["/path/to/file"],
"args": [
"/path/to/file"
],
"options": {},
"output": "<system_message level=\"2\" line=\"0\" source=\"\" type=\"WARNING\">\n <paragraph>\n Include file '/srcdir/path/to/file' not found or reading it failed"
"output": "<system_message level=\"2\" line=\"0\" source=\"notset\" type=\"WARNING\">\n <paragraph>\n Include file '/srcdir/path/to/file' not found or reading it failed"
},
{
"name": "toctree",
Expand All @@ -346,28 +372,36 @@
{
"name": "sectionauthor",
"class": "sphinx.directives.other.Author",
"args": ["bob geldof"],
"args": [
"bob geldof"
],
"options": {},
"output": ""
},
{
"name": "moduleauthor",
"class": "sphinx.directives.other.Author",
"args": ["ringo starr"],
"args": [
"ringo starr"
],
"options": {},
"output": ""
},
{
"name": "codeauthor",
"class": "sphinx.directives.other.Author",
"args": ["paul mcartney"],
"args": [
"paul mcartney"
],
"options": {},
"output": ""
},
{
"name": "index",
"class": "sphinx.directives.other.Index",
"args": ["something"],
"args": [
"something"
],
"options": {},
"output": "<index entries=\"('single',\\ 'something',\\ 'index-0',\\ '',\\ None)\" inline=\"False\">\n<target ids=\"index-0\">"
},
Expand All @@ -382,14 +416,18 @@
{
"name": "tabularcolumns",
"class": "sphinx.directives.other.TabularColumns",
"args": ["spec"],
"args": [
"spec"
],
"options": {},
"output": "<tabular_col_spec spec=\"spec\">"
},
{
"name": "centered",
"class": "sphinx.directives.other.Centered",
"args": ["text"],
"args": [
"text"
],
"options": {},
"output": "<centered>\n text"
},
Expand All @@ -412,21 +450,27 @@
{
"name": "only",
"class": "sphinx.directives.other.Only",
"args": ["expr"],
"args": [
"expr"
],
"options": {},
"output": "<only expr=\"expr\">"
},
{
"name": "include",
"class": "sphinx.directives.other.Include",
"args": ["path/to/include"],
"args": [
"path/to/include"
],
"options": {},
"output": ""
},
{
"name": "figure",
"class": "sphinx.directives.patches.Figure",
"args": ["path/to/figure"],
"args": [
"path/to/figure"
],
"options": {},
"content": "caption\n\nlegend",
"output": "<figure>\n <image uri=\"path/to/figure\">\n <caption>\n caption\n <legend>\n <paragraph>\n legend"
Expand All @@ -450,8 +494,9 @@
"name": "csv-table",
"class": "sphinx.directives.patches.CSVTable",
"args": [],
"options": {},
"output": ""
"options": {"header": "\"Treat\", \"Quantity\", \"Description\""},
"content": "\"Albatross\", 2.99, \"On a stick!\"",
"output": "<table>\n <tgroup cols=\"3\">\n <colspec colwidth=\"33\">\n <colspec colwidth=\"33\">\n <colspec colwidth=\"33\">\n <tbody>\n <row>\n <entry>\n <paragraph>\n Albatross\n <entry>\n <paragraph>\n 2.99\n <entry>\n <paragraph>\n On a stick!"
},
{
"name": "list-table",
Expand All @@ -464,7 +509,9 @@
{
"name": "code",
"class": "sphinx.directives.patches.Code",
"args": ["python"],
"args": [
"python"
],
"options": {},
"content": "a",
"output": "<literal_block force=\"False\" highlight_args=\"{}\" language=\"python\" xml:space=\"preserve\">\n a"
Expand All @@ -479,22 +526,70 @@
{
"name": "deprecated",
"class": "sphinx.domains.changeset.VersionChange",
"args": ["0.3"],
"args": [
"0.3"
],
"options": {},
"output": "<versionmodified type=\"deprecated\" version=\"0.3\">\n <paragraph translatable=\"False\">\n <inline classes=\"versionmodified deprecated\">\n Deprecated since version 0.3."
},
{
"name": "versionadded",
"class": "sphinx.domains.changeset.VersionChange",
"args": ["0.2"],
"args": [
"0.2"
],
"options": {},
"output": "<versionmodified type=\"versionadded\" version=\"0.2\">\n <paragraph translatable=\"False\">\n <inline classes=\"versionmodified added\">\n New in version 0.2."
},
{
"name": "versionchanged",
"class": "sphinx.domains.changeset.VersionChange",
"args": ["0.1"],
"args": [
"0.1"
],
"options": {},
"output": "<versionmodified type=\"versionchanged\" version=\"0.1\">\n <paragraph translatable=\"False\">\n <inline classes=\"versionmodified changed\">\n Changed in version 0.1."
},
{
"name": "glossary",
"class": "sphinx.domains.std.Glossary",
"args": [],
"options": {},
"content": "term 1 : A\nterm 2 : B\n Definition of both terms.",
"output": "<glossary>\n <definition_list classes=\"glossary\">\n <definition_list_item>\n <term ids=\"term-term-1\">\n term 1\n <index entries=\"('single',\\ 'term\\ 1',\\ 'term-term-1',\\ 'main',\\ 'A')\">\n <term ids=\"term-term-2\">\n term 2\n <index entries=\"('single',\\ 'term\\ 2',\\ 'term-term-2',\\ 'main',\\ 'B')\">\n <definition>\n <paragraph>\n Definition of both terms."
},
{
"name": "productionlist",
"class": "sphinx.domains.std.ProductionList",
"args": [
"try_stmt: try1_stmt | try2_stmt"
],
"options": {},
"output": "<productionlist>\n <production ids=\"grammar-token-try-stmt\" tokenname=\"try_stmt\" xml:space=\"preserve\">\n try1_stmt | try2_stmt"
},
{
"name": "cmdoption",
"class": "sphinx.domains.std.Cmdoption",
"args": ["a"],
"options": {},
"output": "<index entries=\"('pair',\\ 'command\\ line\\ option;\\ a',\\ 'cmdoption-arg-a',\\ '',\\ None)\">\n<desc desctype=\"cmdoption\" domain=\"std\" noindex=\"False\" objtype=\"cmdoption\">\n <desc_signature allnames=\"a\" first=\"False\" ids=\"cmdoption-arg-a\" names=\"cmdoption-arg-a\">\n <desc_name xml:space=\"preserve\">\n a\n <desc_addname xml:space=\"preserve\">\n <desc_content>"
},
{
"name": "rst:directive",
"class": "sphinx.domains.rst.ReSTDirective",
"args": [
"a"
],
"options": {},
"output": "<index entries=\"('single',\\ 'a\\ (directive)',\\ 'directive-a',\\ '',\\ None)\">\n<desc desctype=\"directive\" domain=\"rst\" noindex=\"False\" objtype=\"directive\">\n <desc_signature first=\"False\" ids=\"directive-a\" names=\"directive-a\">\n <desc_name xml:space=\"preserve\">\n .. a::\n <desc_content>"
},
{
"name": "rst:directive:option",
"class": "sphinx.domains.rst.ReSTDirectiveOption",
"args": [
"a"
],
"options": {},
"output": "<index entries=\"('single',\\ ':a:\\ (directive\\ option)',\\ 'directive:option--a',\\ '',\\ 'A')\">\n<desc desctype=\"directive:option\" domain=\"rst\" noindex=\"False\" objtype=\"directive:option\">\n <desc_signature first=\"False\" ids=\"directive:option--a\" names=\"directive:option--a\">\n <desc_name xml:space=\"preserve\">\n :a:\n <desc_content>"
}
]
Loading

0 comments on commit 46bf7e1

Please sign in to comment.