def test_markdown_definition_list_advanced(): src = dedent(""" Bla Bla The Term : Its Definition More Definition Even More Another Term : Define this More Text """) expected = dedent(""" <p>Bla Bla</p> <dl> <dt></dt><dt>The Term</dt><dd><p>Its Definition More Definition</p> <p>Even More</p> </dd><dt></dt><dt>Another Term</dt><dd><p>Define this</p> </dd></dl><p>More Text</p> """).strip() print(convert_markdown(src)) assert convert_markdown(src).strip() == expected
def test_markdown_ansi_colors(): src = dedent(""" ```ansi On branch ansicolors Changes to be committed: (use "git reset HEAD <file>..." to unstage) ␛[32mmodified: naucse/markdown_util.py␛[m Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) ␛[31mmodified: test_naucse/test_markdown.py␛[m ``` """) expected = dedent(""" <div class="highlight"><pre><code>On branch ansicolors Changes to be committed: (use "git reset HEAD <file>..." to unstage) <span style="color: #00aa00">modified: naucse/markdown_util.py</span> Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) <span style="color: #aa0000">modified: test_naucse/test_markdown.py</span></code></pre></div> """).strip() assert convert_markdown(src) == expected
def test_markdown_admonition_html(): src = dedent(""" > [note] > Foo <var>bar</var> """) expected = '<div class="admonition note"><p>Foo <var>bar</var></p>\n</div>' assert convert_markdown(src) == expected
def test_markdown_admonition(): src = dedent(""" > [note] > Foo *bar* """) expected = '<div class="admonition note"><p>Foo <em>bar</em></p>\n</div>' assert convert_markdown(src) == expected
def solution(ctx, text): """A solution to a problem. The intent is for the solution to be hidden by default, and only shown after an explicit action by the reader. The explicit action can be: - Clicking a dumb link, which takes the reader to a special page that shows only the solution - Clicking a button, which shows the solution using Javascript To set up the special page, this filter needs special setup in the view. So, it can only be used within lesson pages. """ solutions = ctx['$solutions'] solution_index = len(solutions) solution_url = ctx['lesson_url'](lesson=ctx['lesson'].slug, page=ctx['page'].slug, solution=solution_index) solution = markdown_util.convert_markdown(text) solutions.append(solution) t = Markup(textwrap.dedent(""" <div class="solution" id="solution-{}"> <h3>Řešení</h3> <div class="solution-cover"> <a href="{}"><span class="link-text">Ukázat řešení</span></a> </div> <div class="solution-body" aria-hidden="true"> {} </div> </div> """)) return t.format(solution_index, solution_url, solution)
def test_static_link_conversion(what): text = '[alt](foo/bar)' if what == 'image': text = '!' + text def convert_url(url): return url[::-1] param = 'href' if what == 'link' else 'src' assert '{}="rab/oof"'.format(param) in convert_markdown(text, convert_url)
def test_convert_with_matrix_multiplication(lexer): src = dedent(""" ```{} {}a @ a ``` """.format(lexer, '>>> ' if lexer == 'pycon' else '')) expected = dedent(""" <span class="n">a</span> <span class="o">@</span> <span class="n">a</span> """).strip().replace('\n', '') assert expected in convert_markdown(src).replace('\n', '')
def get_coverpage_content(self, run, coverpage, app): coverpage += ".md" q = self.path / 'sessions' / self.slug / coverpage try: with q.open() as f: md_content = f.read() except FileNotFoundError: return "" html_content = convert_markdown(md_content) return html_content
def test_convert_with_prompt_spaces_console(): src = dedent(""" ```console (__venv__)$ python ``` """) expected = dedent(""" <div class="highlight"><pre><span></span> <span class="gp">(__venv__)$ </span>python </pre></div> """).strip().replace('\n', '') assert convert_markdown(src).replace('\n', '') == expected
def test_markdown_admonition_name_and_title(): src = dedent(""" > [warning] NB! > > foo """) expected = dedent(""" <div class="admonition warning"><p class="admonition-title">NB!</p> <p>foo</p> </div> """).strip() assert convert_markdown(src) == expected
def test_markdown_admonition_name(): src = dedent(""" !!! note "NB!" foo """) expected = dedent(""" <div class="admonition note"><p class="admonition-title">NB!</p> <p>foo</p> </div> """).strip() assert convert_markdown(src) == expected
def test_convert_with_dosvenv_prompt(pre_prompt, space, command): src = dedent(""" ```dosvenv {}>{}{} ``` """).format(pre_prompt, space, command) expected = dedent(""" <div class="highlight"><pre><span></span> <span class="gp">{}>{}</span>{} </pre></div> """).strip().replace('\n', '').format(pre_prompt, space, html.escape(command)) assert convert_markdown(src).replace('\n', '') == expected
def test_markdown_definition_list(): src = dedent(""" Bla Bla The Term : Its Definition More Text """) expected = dedent(""" <p>Bla Bla</p> <dl> <dt></dt><dt>The Term</dt><dd><p>Its Definition</p> </dd></dl><p>More Text</p> """).strip() assert convert_markdown(src).strip() == expected
def test_markdown_admonition_paragraphs(): src = dedent(""" > [note] > > Foo *fi* > > fo > > fum """) expected = dedent(""" <div class="admonition note"><p>Foo <em>fi</em></p> <p>fo</p> <p>fum</p> </div> """).strip() assert convert_markdown(src) == expected
def test_convert_with_prompt_spaces_pycon(): src = dedent(""" ```pycon >>> def foo(req): ... return req ``` """) expected = dedent(""" <div class="highlight"><pre><span></span> <span class="gp">>>> </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span> <span class="n">req</span><span class="p">):</span> <span class="gp">... </span> <span class="k">return</span> <span class="n">req</span> </pre></div> """).strip().replace('\n', '') assert convert_markdown(src).replace('\n', '') == expected
def test_markdown_admonition_code(): src = dedent(""" > [note] NB! > > foo > ```python > cat = Kitty() > cat.make_sound() > ``` """) expected = dedent(""" <div class="admonition note"><p class="admonition-title">NB!</p> <p>foo</p> <div class="highlight"><pre><span></span><span class="n">cat</span> <span class="o">=</span> <span class="n">Kitty</span><span class="p">()</span> <span class="n">cat</span><span class="o">.</span><span class="n">make_sound</span><span class="p">()</span> </pre></div></div> """).strip() assert convert_markdown(src) == expected
def test_convert_full_dosvenv_prompt(): src = dedent(r""" ```dosvenv > whoami helena > venv\Scripts\activate # activate virtualenv (venv)> dir Directory of C:\Users\helena 05/08/2014 07:28 PM <DIR> Desktop ``` """) expected = dedent(r""" <div class="highlight"><pre><span></span><span class="gp">> </span>whoami <span class="go">helena</span><span class="gp"></span> <span class="gp">> </span>venv\Scripts\activate <span class="c"># activate virtualenv</span> <span class="gp">(venv)> </span>dir <span class="go"> Directory of C:\Users\helena</span> <span class="go">05/08/2014 07:28 PM <DIR> Desktop</span> </pre></div> """).strip() print(expected) assert convert_markdown(src) == expected
def render_html(self, solution=None, static_url=None, lesson_url=None, vars=None, ): lesson = self.lesson if not vars: vars = {} else: vars = dict(vars) vars.update(self.vars) solutions = [] if static_url is None: def static_url(path): return 'static/{}'.format(path) if lesson_url is None: def lesson_url(lesson, page='index', solution=None): lesson = self.root.get_lesson(lesson) url = '../../{}/'.format(lesson.slug) if page != 'index' or solution is not None: url += '{}/'.format(page) if solution is not None: url += '{}/'.format(solution) return url def default_lesson_url(lesson, page='index'): url = '../../{}/'.format(lesson.slug) if page != 'index': url += '' return 'static/{}'.format(path) kwargs = { 'static': lambda path: static_url(path), 'lesson_url': lambda lesson, page='index', solution=None: lesson_url(lesson=lesson, page=page, solution=solution), 'subpage_url': lambda page: lesson_url(lesson=lesson, page=page), 'lesson': lesson, 'page': self, '$solutions': solutions, } kwargs.update(vars_functions(vars)) if self.jinja: template = self._get_template() content = template.render(**kwargs) else: with self.path.open() as file: content = file.read() def convert_url(url): prefix = 'static/' if not url.startswith(prefix): return url return static_url(url[len(prefix):]) if self.style == 'md': content = jinja2.Markup(convert_markdown(content, convert_url)) elif self.style == 'ipynb': content = jinja2.Markup(convert_notebook(content, convert_url)) else: template = self._get_template() content = jinja2.Markup(content) if solution is None: return content else: return solutions[solution]
def markdown(text, inline=False): return markdown_util.convert_markdown(text, inline=inline)
def test_markdown_keeps_nbsp(): text = 'Some text\N{NO-BREAK SPACE}more text' assert convert_markdown(text).strip() == '<p>{}</p>'.format(text)