Datasets:
Download docs/books-markdown-processing/test_format.py from xytpai/datax-open: direct link, hf CLI and curl.
- Browser
- Download file 2.04 kB
-
https://huggingface.co/datasets/xytpai/datax-open/resolve/main/docs/books-markdown-processing/test_format.py
- Command line
-
hf download hf://datasets/xytpai/datax-open/docs/books-markdown-processing/test_format.py
-
curl -L -o test_format.py https://huggingface.co/datasets/xytpai/datax-open/resolve/main/docs/books-markdown-processing/test_format.py
2.04 kB
| from format_markdown import transform | |
| cases=[ | |
| ("chapter","CHAPTER IV\n\nThe story begins.","## CHAPTER IV"), | |
| ("named","Preface\n\nSome opening prose.","## Preface"), | |
| ("html_heading","<h2>Methods</h2>\n<p>A plain paragraph.</p>","## Methods"), | |
| ("list","• First item\n• Second item","- First item"), | |
| ("html_list","<ol><li>First</li><li>Second</li></ol>","1. First"), | |
| ("table","<table><tr><th>Time</th><th>Value</th></tr><tr><td>1</td><td>3</td></tr></table>","| Time | Value |"), | |
| ("pre","<pre>if x < y:\n print(x)</pre>","if x < y:"), | |
| ] | |
| for name,text,expect in cases: | |
| out,edits=transform(text) | |
| assert expect in out,(name,out) | |
| print(name,edits) | |
| protected=[ | |
| "```python\nCHAPTER IV\n\n<p>literal</p>\n```\n", | |
| "$$\nx = 3 \\\\\ny=2\n$$\n", | |
| " <p>code literal</p>\n", | |
| "<table><tr><td colspan='2'>Do not infer</td></tr></table>", | |
| "An inline ` <b>code</b> ` example.", | |
| ] | |
| for text in protected: | |
| out,_=transform(text) | |
| assert out==text,(text,out) | |
| poetry="The rivers flow beneath the moon,\nAnd stars shall sing a silver tune.\n" | |
| assert transform(poetry,"Poems")[0]==poetry | |
| print('protected code/math/complex-table/poetry: OK') | |
| # Typical wrapped prose, containing real sentences, must preserve all tokens. | |
| import textwrap | |
| paragraph=("This is an ordinary prose paragraph with a sequence of clear explanatory statements. " | |
| "The following words continue the explanation without introducing any code or mathematical notation. ")*8 | |
| wrapped=textwrap.fill(paragraph,72) | |
| out,e=transform(wrapped,"An ordinary book") | |
| assert out.split()==wrapped.split() | |
| assert e.get('prose_hardwraps_joined',0)>0 | |
| print('prose whitespace-only join',e) | |
| # Regression: do not render instructional HTML or promote references to headings. | |
| for raw in ['Appendix B shows an equation of the form \\(x=1\\) can be rearranged:', 'Here is an HTML example:\n<h1>Heading</h1>\n<p>Example</p>', '| unordered list | <ul><li>item 1</li><li>item 2</li></ul> |', '<!DOCTYPE html> <html><body><p>Example</p></body></html>']: | |
| assert transform(raw)[0]==raw | |