init
This commit is contained in:
commit
38355d2442
9083 changed files with 1225834 additions and 0 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
172
.venv/lib/python3.8/site-packages/commonmark/tests/rst_tests.py
Normal file
172
.venv/lib/python3.8/site-packages/commonmark/tests/rst_tests.py
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
import unittest
|
||||
|
||||
import commonmark
|
||||
|
||||
|
||||
class TestCommonmark(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.parser = commonmark.Parser()
|
||||
self.renderer = commonmark.ReStructuredTextRenderer()
|
||||
|
||||
def render_rst(self, test_str):
|
||||
ast = self.parser.parse(test_str)
|
||||
rst = self.renderer.render(ast)
|
||||
|
||||
return rst
|
||||
|
||||
def assertEqualRender(self, src_markdown, expected_rst):
|
||||
rendered_rst = self.render_rst(src_markdown)
|
||||
self.assertEqual(rendered_rst, expected_rst)
|
||||
|
||||
def test_strong(self):
|
||||
src_markdown = 'Hello **Strong**'
|
||||
expected_rst = '\nHello **Strong**\n'
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_emphasis(self):
|
||||
src_markdown = 'Hello *Emphasis*'
|
||||
expected_rst = '\nHello *Emphasis*\n'
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_paragraph(self):
|
||||
src_markdown = 'Hello paragraph'
|
||||
expected_rst = '\nHello paragraph\n'
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_link(self):
|
||||
src_markdown = '[Link](http://example.com)'
|
||||
expected_rst = '\n`Link <http://example.com>`_\n'
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_image(self):
|
||||
src_markdown = ''
|
||||
expected_rst = """
|
||||
.. image:: http://placekitten.com/100/100
|
||||
:alt: Image
|
||||
"""
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_code(self):
|
||||
src_markdown = 'Test `inline code` with backticks'
|
||||
expected_rst = '\nTest ``inline code`` with backticks\n'
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_code_block(self):
|
||||
src_markdown = """
|
||||
```python
|
||||
# code block
|
||||
print '3 backticks or'
|
||||
print 'indent 4 spaces'
|
||||
```
|
||||
"""
|
||||
expected_rst = """
|
||||
.. code:: python
|
||||
|
||||
# code block
|
||||
print '3 backticks or'
|
||||
print 'indent 4 spaces'
|
||||
"""
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_unordered_list(self):
|
||||
src_markdown = """
|
||||
This is a list:
|
||||
* List item
|
||||
* List item
|
||||
* List item
|
||||
"""
|
||||
expected_rst = """
|
||||
This is a list:
|
||||
|
||||
* List item
|
||||
* List item
|
||||
* List item
|
||||
"""
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_ordered_list(self):
|
||||
src_markdown = """
|
||||
This is a ordered list:
|
||||
1. One
|
||||
2. Two
|
||||
3. Three
|
||||
"""
|
||||
expected_rst = """
|
||||
This is a ordered list:
|
||||
|
||||
#. One
|
||||
#. Two
|
||||
#. Three
|
||||
"""
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_block_quote(self):
|
||||
src_markdown = """
|
||||
Before the blockquote:
|
||||
|
||||
> The blockquote
|
||||
|
||||
After the blockquote
|
||||
"""
|
||||
expected_rst = """
|
||||
Before the blockquote:
|
||||
|
||||
The blockquote
|
||||
|
||||
After the blockquote
|
||||
"""
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_heading(self):
|
||||
src_markdown = '''
|
||||
# Heading 1
|
||||
|
||||
## Heading 2
|
||||
|
||||
### Heading 3
|
||||
|
||||
#### Heading 4
|
||||
|
||||
##### Heading 5
|
||||
|
||||
###### Heading 6
|
||||
'''
|
||||
expected_rst = '''
|
||||
Heading 1
|
||||
#########
|
||||
|
||||
Heading 2
|
||||
*********
|
||||
|
||||
Heading 3
|
||||
=========
|
||||
|
||||
Heading 4
|
||||
---------
|
||||
|
||||
Heading 5
|
||||
^^^^^^^^^
|
||||
|
||||
Heading 6
|
||||
"""""""""
|
||||
'''
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
def test_multiple_paragraphs(self):
|
||||
src_markdown = '''
|
||||
Start of first paragraph that
|
||||
continues on a new line
|
||||
|
||||
This is the second paragraph
|
||||
'''
|
||||
expected_rst = '''
|
||||
Start of first paragraph that
|
||||
continues on a new line
|
||||
|
||||
This is the second paragraph
|
||||
'''
|
||||
self.assertEqualRender(src_markdown, expected_rst)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
@ -0,0 +1,242 @@
|
|||
#!/usr/bin/env python
|
||||
# coding: utf-8
|
||||
from __future__ import division, print_function, unicode_literals
|
||||
import re
|
||||
import timeit
|
||||
import codecs
|
||||
import argparse
|
||||
import sys
|
||||
from builtins import str
|
||||
from commonmark.render.html import HtmlRenderer
|
||||
from commonmark.main import Parser, dumpAST
|
||||
|
||||
|
||||
class colors(object):
|
||||
HEADER = '\033[95m'
|
||||
OKBLUE = '\033[94m'
|
||||
OKGREEN = '\033[92m'
|
||||
WARNING = '\033[93m'
|
||||
FAIL = '\033[91m'
|
||||
ENDC = '\033[0m'
|
||||
|
||||
|
||||
def trace_calls(frame, event, arg):
|
||||
co = frame.f_code
|
||||
func_name = co.co_name
|
||||
if func_name == "write":
|
||||
return
|
||||
line_no = frame.f_lineno
|
||||
filename = co.co_filename
|
||||
if event == "call" and not re.match("__", func_name) and \
|
||||
re.search("CommonMark.py", filename) \
|
||||
and func_name != "dumpAST":
|
||||
print("-> " + frame.f_back.f_code.co_name +
|
||||
" at " + str(frame.f_back.f_lineno) +
|
||||
" called " + func_name + " at " + str(line_no) +
|
||||
" in " + filename)
|
||||
return trace_calls
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="script to run the CommonMark specification tests " +
|
||||
"against the CommonMark.py parser")
|
||||
parser.add_argument(
|
||||
'-t',
|
||||
help="Single test to run or comma separated list " +
|
||||
"of tests (-t 10 or -t 10,11,12,13)")
|
||||
parser.add_argument(
|
||||
'-p',
|
||||
action="store_true",
|
||||
help="Print passed test information")
|
||||
parser.add_argument(
|
||||
'-f',
|
||||
action="store_true",
|
||||
help="Print failed tests (during -np...)")
|
||||
parser.add_argument(
|
||||
'-i',
|
||||
action="store_true",
|
||||
help="Interactive Markdown input mode")
|
||||
parser.add_argument(
|
||||
'-d',
|
||||
action="store_true",
|
||||
help="Debug, trace calls")
|
||||
parser.add_argument(
|
||||
'-np',
|
||||
action="store_true",
|
||||
help="Only print section header, tick, or cross")
|
||||
parser.add_argument(
|
||||
'-s',
|
||||
action="store_true",
|
||||
help="Print percent of tests passed by category")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.d:
|
||||
sys.settrace(trace_calls)
|
||||
|
||||
renderer = HtmlRenderer()
|
||||
parser = Parser()
|
||||
|
||||
f = codecs.open("spec.txt", encoding="utf-8")
|
||||
datalist = []
|
||||
for line in f:
|
||||
datalist.append(line)
|
||||
data = "".join(datalist)
|
||||
passed = 0
|
||||
failed = 0
|
||||
catStats = {}
|
||||
examples = []
|
||||
example_number = 0
|
||||
current_section = ""
|
||||
tabChar = '\u2192'
|
||||
spaceChar = '\u2423'
|
||||
nbspChar = '\u00A0'
|
||||
|
||||
def showSpaces(t):
|
||||
t = re.sub("\\t", tabChar, t)
|
||||
t = re.sub(" ", spaceChar, t)
|
||||
t = re.sub(nbspChar, spaceChar, t)
|
||||
return t
|
||||
|
||||
t = re.sub("\r\n", "\n", data)
|
||||
|
||||
tests = re.sub(
|
||||
re.compile("^<!-- END TESTS -->(.|[\n])*", flags=re.M), '', t)
|
||||
testMatch = re.findall(
|
||||
re.compile(
|
||||
r'^`{32} example\n'
|
||||
r'([\s\S]*?)^\.\n([\s\S]*?)'
|
||||
r'^`{32}$'
|
||||
r'|^#{1,6} *(.*)$',
|
||||
re.M),
|
||||
tests)
|
||||
|
||||
for match in testMatch:
|
||||
if not match[2] == "":
|
||||
current_section = match[2]
|
||||
else:
|
||||
example_number += 1
|
||||
examples.append({
|
||||
'markdown': match[0],
|
||||
'html': match[1],
|
||||
'section': current_section,
|
||||
'number': example_number})
|
||||
|
||||
current_section = ""
|
||||
|
||||
startTime = timeit.default_timer()
|
||||
|
||||
if args.i:
|
||||
print(
|
||||
colors.OKGREEN +
|
||||
"(To end input of Markdown block enter 'end' on " +
|
||||
"it's own line, to quit enter 'quit')" +
|
||||
colors.ENDC)
|
||||
while True:
|
||||
s = ""
|
||||
while True:
|
||||
if sys.version_info >= (3, 0):
|
||||
inp = input(colors.OKBLUE + 'Markdown: ' + colors.ENDC)
|
||||
else:
|
||||
inp = raw_input(colors.OKBLUE + 'Markdown: ' + colors.ENDC) # noqa
|
||||
|
||||
if not inp == "end" and inp != "quit":
|
||||
s += inp + "\n"
|
||||
elif inp == "end":
|
||||
s = s[:-1]
|
||||
break
|
||||
elif inp == "quit":
|
||||
print(colors.HEADER+"bye!"+colors.ENDC)
|
||||
exit(0)
|
||||
ast = parser.parse(s)
|
||||
html = renderer.render(ast)
|
||||
print(colors.WARNING+"="*10+"AST====="+colors.ENDC)
|
||||
dumpAST(ast)
|
||||
print(colors.WARNING+"="*10+"HTML===="+colors.ENDC)
|
||||
print(html)
|
||||
|
||||
# some tests?
|
||||
if args.t:
|
||||
tests = args.t.split(",")
|
||||
choice_examples = []
|
||||
for t in tests:
|
||||
if not t == "" and len(examples) > int(t):
|
||||
choice_examples.append(examples[int(t)-1])
|
||||
examples = choice_examples
|
||||
|
||||
# all tests
|
||||
|
||||
for i, example in enumerate(examples): # [0,examples[0]]
|
||||
if not example['section'] == "" and \
|
||||
not current_section == example['section']:
|
||||
print('\n' + colors.HEADER + '[' + example['section'] + ']' +
|
||||
colors.ENDC + ' ', end='')
|
||||
current_section = example['section']
|
||||
catStats.update({current_section: [0, 0, 0]})
|
||||
|
||||
catStats[current_section][2] += 1
|
||||
if args.d:
|
||||
print(colors.HEADER+"[Parsing]"+colors.ENDC)
|
||||
ast = parser.parse(re.sub(tabChar, "\t", example['markdown']))
|
||||
if args.d:
|
||||
print(colors.HEADER+"[Rendering]"+colors.ENDC)
|
||||
actual = renderer.render(ast)
|
||||
if re.sub('\t', tabChar, actual) == example['html']:
|
||||
passed += 1
|
||||
catStats[current_section][0] += 1
|
||||
if not args.f:
|
||||
print(colors.OKGREEN + '✓' + colors.ENDC, end='')
|
||||
if args.d:
|
||||
dumpAST(ast)
|
||||
if args.p or args.d and not args.np:
|
||||
print(
|
||||
colors.OKBLUE +
|
||||
"=== markdown ===============\n" +
|
||||
colors.ENDC + showSpaces(example['markdown']) +
|
||||
colors.OKBLUE +
|
||||
"\n=== expected ===============\n" +
|
||||
colors.ENDC + showSpaces(example['html']) +
|
||||
colors.OKBLUE +
|
||||
"\n=== got ====================\n" +
|
||||
colors.ENDC + showSpaces(actual))
|
||||
else:
|
||||
failed += 1
|
||||
catStats[current_section][1] += 1
|
||||
if args.t:
|
||||
print("Test #" + str(args.t.split(",")[i]), end='')
|
||||
else:
|
||||
print("Test #" + str(i+1), end='')
|
||||
print(' ' + colors.FAIL + "✗" + colors.ENDC)
|
||||
if args.d:
|
||||
dumpAST(ast)
|
||||
if not args.np or args.f:
|
||||
print(
|
||||
colors.WARNING +
|
||||
"=== markdown ===============\n" +
|
||||
colors.ENDC + showSpaces(example['markdown']) +
|
||||
colors.WARNING +
|
||||
"\n=== expected ===============\n" +
|
||||
colors.ENDC + showSpaces(example['html']) +
|
||||
colors.WARNING +
|
||||
"\n=== got ====================\n" +
|
||||
colors.ENDC + showSpaces(actual))
|
||||
|
||||
print('\n' + str(passed) + ' tests passed, ' + str(failed) + ' failed')
|
||||
|
||||
endTime = timeit.default_timer()
|
||||
runTime = endTime - startTime
|
||||
|
||||
if args.s:
|
||||
for i in catStats.keys():
|
||||
per = catStats[i][0]/catStats[i][2]
|
||||
print(colors.HEADER + "[" + i + "]" + colors.ENDC +
|
||||
"\t" + str(per*100) + "% Passed")
|
||||
|
||||
print("runtime: " + str(runTime) + "s")
|
||||
|
||||
if (failed > 0):
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
157
.venv/lib/python3.8/site-packages/commonmark/tests/unit_tests.py
Normal file
157
.venv/lib/python3.8/site-packages/commonmark/tests/unit_tests.py
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import unittest
|
||||
|
||||
try:
|
||||
from hypothesis import given, example
|
||||
except ImportError:
|
||||
# Mock out hypothesis stuff for python 2.6
|
||||
def given(a):
|
||||
def func(b):
|
||||
return
|
||||
return func
|
||||
|
||||
example = given
|
||||
|
||||
try:
|
||||
from hypothesis.strategies import text
|
||||
except ImportError:
|
||||
def text():
|
||||
pass
|
||||
|
||||
|
||||
import commonmark
|
||||
from commonmark.blocks import Parser
|
||||
from commonmark.render.html import HtmlRenderer
|
||||
from commonmark.inlines import InlineParser
|
||||
from commonmark.node import NodeWalker, Node
|
||||
|
||||
|
||||
class TestCommonmark(unittest.TestCase):
|
||||
def test_output(self):
|
||||
s = commonmark.commonmark('*hello!*')
|
||||
self.assertEqual(s, '<p><em>hello!</em></p>\n')
|
||||
|
||||
def test_unicode(self):
|
||||
s = commonmark.commonmark('<div>\u2020</div>\n')
|
||||
self.assertEqual(s, '<div>\u2020</div>\n',
|
||||
'Unicode works in an HTML block.')
|
||||
commonmark.commonmark('* unicode: \u2020')
|
||||
commonmark.commonmark('# unicode: \u2020')
|
||||
commonmark.commonmark('```\n# unicode: \u2020\n```')
|
||||
|
||||
def test_null_string_bug(self):
|
||||
s = commonmark.commonmark('> sometext\n>\n\n')
|
||||
self.assertEqual(
|
||||
s,
|
||||
'<blockquote>\n<pre><code>sometext\n</code></pre>'
|
||||
'\n</blockquote>\n')
|
||||
|
||||
def test_normalize_contracts_text_nodes(self):
|
||||
md = '_a'
|
||||
ast = Parser().parse(md)
|
||||
|
||||
def assert_text_literals(text_literals):
|
||||
walker = ast.walker()
|
||||
document, _ = walker.next()
|
||||
self.assertEqual(document.t, 'document')
|
||||
paragraph, _ = walker.next()
|
||||
self.assertEqual(paragraph.t, 'paragraph')
|
||||
for literal in text_literals:
|
||||
text, _ = walker.next()
|
||||
self.assertEqual(text.t, 'text')
|
||||
self.assertEqual(text.literal, literal)
|
||||
paragraph, _ = walker.next()
|
||||
self.assertEqual(paragraph.t, 'paragraph')
|
||||
|
||||
assert_text_literals(['_', 'a'])
|
||||
ast.normalize()
|
||||
# assert text nodes are contracted
|
||||
assert_text_literals(['_a'])
|
||||
ast.normalize()
|
||||
# assert normalize() doesn't alter a normalized ast
|
||||
assert_text_literals(['_a'])
|
||||
|
||||
def test_dumpAST_orderedlist(self):
|
||||
md = '1.'
|
||||
ast = Parser().parse(md)
|
||||
commonmark.dumpAST(ast)
|
||||
|
||||
@given(text())
|
||||
def test_random_text(self, s):
|
||||
commonmark.commonmark(s)
|
||||
|
||||
def test_smart_dashes(self):
|
||||
md = 'a - b -- c --- d ---- e ----- f'
|
||||
EM = '\u2014'
|
||||
EN = '\u2013'
|
||||
expected_html = (
|
||||
'<p>'
|
||||
+ 'a - '
|
||||
+ 'b ' + EN + ' '
|
||||
+ 'c ' + EM + ' '
|
||||
+ 'd ' + EN + EN + ' '
|
||||
+ 'e ' + EM + EN + ' '
|
||||
+ 'f</p>\n')
|
||||
parser = commonmark.Parser(options=dict(smart=True))
|
||||
ast = parser.parse(md)
|
||||
renderer = commonmark.HtmlRenderer()
|
||||
html = renderer.render(ast)
|
||||
self.assertEqual(html, expected_html)
|
||||
|
||||
def test_regex_vulnerability_link_label(self):
|
||||
i = 200
|
||||
while i <= 2000:
|
||||
s = commonmark.commonmark('[' + ('\\' * i) + '\n')
|
||||
self.assertEqual(s, '<p>' + '[' + ('\\' * (i // 2)) + '</p>\n',
|
||||
'[\\\\... %d deep' % (i,))
|
||||
i *= 10
|
||||
|
||||
def test_regex_vulnerability_link_destination(self):
|
||||
i = 200
|
||||
while i <= 2000:
|
||||
s = commonmark.commonmark(('[](' * i) + '\n')
|
||||
self.assertEqual(s, '<p>' + ('[](' * i) + '</p>\n',
|
||||
'[]( %d deep' % (i,))
|
||||
i *= 10
|
||||
|
||||
|
||||
class TestHtmlRenderer(unittest.TestCase):
|
||||
def test_init(self):
|
||||
HtmlRenderer()
|
||||
|
||||
|
||||
class TestInlineParser(unittest.TestCase):
|
||||
def test_init(self):
|
||||
InlineParser()
|
||||
|
||||
|
||||
class TestNode(unittest.TestCase):
|
||||
def test_doc_node(self):
|
||||
Node('document', [[1, 1], [0, 0]])
|
||||
|
||||
|
||||
class TestNodeWalker(unittest.TestCase):
|
||||
def test_node_walker(self):
|
||||
node = Node('document', [[1, 1], [0, 0]])
|
||||
NodeWalker(node)
|
||||
|
||||
def test_node_walker_iter(self):
|
||||
node = Node('document', [[1, 1], [0, 0]])
|
||||
for subnode, entered in node.walker():
|
||||
pass
|
||||
|
||||
|
||||
class TestParser(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.parser = Parser()
|
||||
|
||||
@given(text())
|
||||
@example('')
|
||||
@example('* unicode: \u2020')
|
||||
def test_text(self, s):
|
||||
self.parser.parse(s)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue