def test_strategy_consistency(): """ Test that the seller strategy specified in the documentation is the same we use in the tests. """ markdown_parser = mistune.create_markdown(renderer=mistune.AstRenderer()) skill_doc_file = Path(ROOT_DIR, "docs", "orm-integration.md") doc = markdown_parser(skill_doc_file.read_text()) # get only code blocks code_blocks = list(filter(lambda x: x["type"] == "block_code", doc)) python_code_blocks = list( filter( lambda x: x["info"] is not None and x["info"].strip() == "python", code_blocks, ) ) strategy_file_content = ORM_SELLER_STRATEGY_PATH.read_text() for python_code_block in python_code_blocks: if not python_code_block["text"] in strategy_file_content: pytest.fail( "Code block not present in strategy file:\n{}".format( python_code_block["text"] ) )
def find_css(body): # parse markdown md = mistune.create_markdown() # alternative algorithm: disable fenced code # see https://github.com/Lachcim/css-irl-bot/issues/5 for discussion # md.block.rules.remove("fenced_code") html = md(body) # it is known that when one parses html with regex, zalgo sings the song # that ends the world. in this case, however, the html produced by mistune # can be assumed to be regular and therefore parseable using regex. # find code blocks # expression = re.compile("<pre><code>(.*?)</code></pre>", re.DOTALL) expression = re.compile("<pre><code[^>]*>(.*?)</code></pre>", re.DOTALL) css = "".join(re.findall(expression, html)) if css: return css, "block" # if the above failed, find inline code expression = re.compile("<code>(.*?)</code>", re.DOTALL) css = "\n".join(re.findall(expression, html)) if css: return css, "inline" # if all failed, parse the entire comment return body, "body"
def test_note_admonition(self): md = create_markdown(plugins=[Admonition()]) s = '.. note:: Warnning\n\n message' html = md(s) self.assertIn('class="admonition note"', html) self.assertIn('<h1>Warnning</h1>', html) self.assertIn('<p>message</p>', html)
def delete(map_identifier, note_identifier): topic_store = get_topic_store() topic_map = topic_store.get_map(map_identifier, current_user.id) if topic_map is None: abort(404) # If the map doesn't belong to the user and they don't have the right # collaboration mode on the map, then abort if not topic_map.owner and ( topic_map.collaboration_mode is not CollaborationMode.EDIT and topic_map.collaboration_mode is not CollaborationMode.COMMENT): abort(403) topic = topic_store.get_topic( map_identifier, "notes", resolve_attributes=RetrievalMode.RESOLVE_ATTRIBUTES) if topic is None: abort(404) note_occurrence = topic_store.get_occurrence( map_identifier, note_identifier, inline_resource_data=RetrievalMode.INLINE_RESOURCE_DATA, resolve_attributes=RetrievalMode.RESOLVE_ATTRIBUTES, ) form_note_title = note_occurrence.get_attribute_by_name("title").value markdown = mistune.create_markdown( renderer=HighlightRenderer(escape=False), plugins=[ "strikethrough", "footnotes", "table", ], ) form_note_text = markdown(note_occurrence.resource_data.decode()) form_note_scope = note_occurrence.scope if request.method == "POST": topic_store.delete_occurrence(map_identifier, note_occurrence.identifier) flash("Note successfully deleted.", "warning") return redirect( url_for( "note.index", map_identifier=topic_map.identifier, topic_identifier=topic.identifier, )) return render_template( "note/delete.html", topic_map=topic_map, topic=topic, note_identifier=note_occurrence.identifier, note_title=form_note_title, note_text=form_note_text, note_scope=form_note_scope, )
def test_code_blocks_all_present(): """ Test that all the code blocks in the docs (aries-cloud-agent-example.md) are present in the Aries test module (tests/test_examples/test_http_client_connection_to_aries_cloud_agent.py). """ markdown_parser = mistune.create_markdown(renderer=mistune.AstRenderer()) skill_doc_file = Path(ROOT_DIR, "docs", "aries-cloud-agent-example.md") doc = markdown_parser(skill_doc_file.read_text()) # get only code blocks offset = 1 code_blocks = list(filter(lambda x: x["type"] == "block_code", doc))[offset:] expected_code_path = Path( ROOT_DIR, "tests", "test_examples", "test_http_client_connection_to_aries_cloud_agent.py", ) expected_code = expected_code_path.read_text() # all code blocks must be present in the expected code for code_block in code_blocks: text = code_block["text"] if text.strip() not in expected_code: pytest.fail("The following code cannot be found in {}:\n{}".format( expected_code_path, text))
def test_code_admonition(self): md = create_markdown(plugins=[Admonition()]) s = '.. note:: Warnning\n\n print() ' html = md(s) self.assertIn('class="admonition note"', html) self.assertIn('<h1>Warnning</h1>', html) self.assertIn('<pre><code>print() \n</code></pre>', html)
def __init__( self, url, username, api_token, page_title_prefix, markdown_dir, db_path, space, parent_pageid, force_update=False, force_delete=False, skip_update=False, verbose=False): self.api = Confluence(url=url, username=username, password=api_token) self.page_title_prefix = page_title_prefix self.markdown_dir = markdown_dir self.kv = KeyValue(db_path) self.space = space self.parent_pageid = parent_pageid self.force_update = force_update self.force_delete = force_delete self.skip_update = skip_update self.confluence_renderer = ConfluenceRenderer(verbose) self.renderer = mistune.create_markdown( renderer=self.confluence_renderer, plugins=[ plugin_front_matter, DirectiveInclude(), HugoRefLinkPlugin(self.markdown_dir), 'strikethrough', 'footnotes', 'table', 'url', Admonition(), plugin_html_comment, ] )
def convert_github_markdown_to_asana_xml(text: str) -> str: markdown = mistune.create_markdown( renderer=GithubToAsanaRenderer(escape=False), plugins=["strikethrough"], ) return markdown(text)
def GenerateXML(input_file: str, dictionary_name: str): """ Generates the Apple XML Source file used for the dictionary. Reads from the given md file and writes it to <dictionary_name>.xml in the current working directory. """ # Read the md file. with open(input_file, "r") as f: contents = f.read() print(contents) # Generate the contents of the dictionary. md_html = mistune.create_markdown(renderer=DictionaryRenderer(), plugins=[DictionaryDirective()]) # Output file. output_file = dictionary_name output_file.replace(" ", "-") output_file += ".xml" # Write to the file with the appropriate surrounding dictionary matter. with open(output_file, "w") as f: f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") f.write( "<d:dictionary xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:d=\"http://www.apple.com/DTDs/DictionaryService-1.0.rng\">\n" ) f.write(md_html(contents)) f.write("</d:dictionary>")
def _notify_reporter(cls, *, message: O365.Message, key: str): """Notify ticket reporter. :param message: the reported issue message :param key: the ticket key """ # creating notification message to be sent to all recipients markdown = mistune.create_markdown(escape=False, hard_wrap=True) body = markdown( TicketService.create_message_body( template='notification.j2', values={ 'summary': message.subject, 'key': key, 'url': current_app.config['TICKET_CLIENT_APP'] })) reply = cls.create_reply( message, values={ 'body': body, 'metadata': [dict(name='message', content='jira ticket notification')] }) reply.send() return reply
def __init__( self, url, username, apiToken, pageTitlePrefix, markdownDir, dbPath, space, parentPageId, forceUpdate=False, forceDelete=False, skipUpdate=False, ): self.api = Confluence(url=url, username=username, password=apiToken) self.pageTitlePrefix = pageTitlePrefix self.markdownDir = markdownDir self.kv = KeyValue(dbPath) self.space = space self.parentPageId = parentPageId self.forceUpdate = forceUpdate self.forceDelete = forceDelete self.skipUpdate = skipUpdate self.confluenceRenderer = ConfluenceRenderer(url) self.metadataPlugin = MetadataPlugin() self.renderer = mistune.create_markdown( renderer=self.confluenceRenderer, plugins=[ 'strikethrough', 'footnotes', 'table', 'url', self.metadataPlugin.plugin_metadata ]) # Hack to allow metadata plugin to work (See mistune/block_parser.py) self.renderer.block.rules.remove('thematic_break')
def make_renderer(classes, plugins): RenderCls = type('FlaskBBRenderer', tuple(classes), {}) markup = mistune.create_markdown(renderer=RenderCls(), plugins=plugins, escape=True, hard_wrap=True) return lambda text: Markup(markup(text))
def test_html_include(self): md = create_markdown(escape=False, plugins=[DirectiveInclude()]) html = md.read(os.path.join(ROOT, 'include/text.md')) self.assertIn('Could not include self', html) self.assertIn('Could not find file', html) self.assertIn('<div>include html</div>', html) self.assertIn('<blockquote>', html) self.assertIn('# Table of Contents', html)
def setup_class(cls): """Set up the test.""" markdown_parser = mistune.create_markdown( renderer=mistune.AstRenderer()) cls.doc_path = cls.DOC_PATH cls.doc_content = cls.doc_path.read_text() cls.blocks = markdown_parser(cls.doc_content) cls.code_blocks = list(filter(block_code_filter, cls.blocks))
def convert_markdown(text: str): """Convert text to markdown HTML.""" markdown = mistune.create_markdown( renderer=HighlightRenderer(), plugins=["footnotes", "strikethrough", "table"], escape=False, ) return markdown(text)
def setup_class(cls): """Set the test up.""" markdown_parser = mistune.create_markdown(renderer=mistune.AstRenderer()) ledger_doc_file = Path(ROOT_DIR, "docs", "ledger-integration.md") doc = markdown_parser(ledger_doc_file.read_text()) # get only code blocks cls.code_blocks = list(filter(lambda x: x["type"] == "block_code", doc))
def test_note_admonition_no_text(self): md = create_markdown( plugins=[Admonition()] ) s = '.. note:: Warnning' html = md(s) self.assertIn('class="admonition note"', html) self.assertIn('<h1>Warnning</h1>', html)
def test_ast_admonition(self): data = fixtures.load_json('admonition.json') md = create_markdown( renderer='ast', plugins=[Admonition()] ) # Use JSON to fix the differences between tuple and list tokens = json.loads(json.dumps(md(data['text']))) self.assertEqual(tokens, data['tokens'])
def test_ast_include(self): md = create_markdown(renderer='ast', plugins=[DirectiveInclude()]) filepath = os.path.join(ROOT, 'include/foo.txt') s = '.. include:: hello.txt' tokens = md.parse(s, {'__file__': filepath}) token = tokens[0] self.assertEqual(token['type'], 'include') self.assertEqual(token['text'], 'hello\n') self.assertEqual(token['relpath'], 'hello.txt')
def test_escape_html(self): md = mistune.create_markdown(escape=True) result = md('<div>1</div>') expected = '<p><div>1</div></p>' self.assertEqual(result.strip(), expected) result = md('<em>1</em>') expected = '<p><em>1</em></p>' self.assertEqual(result.strip(), expected)
def main(filename): with open(filename) as sf_kb: sf_kb_file = sf_kb.read() markdown = mistune.create_markdown(renderer=sf_html_render()) sf_html = open(os.path.splitext(filename)[0] + ".html", "w") sf_html.write(markdown(sf_kb_file))
def make_markdown(update, context): if not update.message or not update.message.text: return markdown = mistune.create_markdown(renderer=TGHtmlRenderer(escape=False), plugins=['strikethrough']) entities = update.message.parse_entities() text = parse_entities_to_html(update.message.text, entities) update.message.reply_text(markdown(text), parse_mode=telegram.ParseMode.HTML, disable_web_page_preview=True)
def extract_code_blocks(filepath, filter_=None): """Extract code blocks from .md files.""" content = Path(filepath).read_text(encoding="utf-8") markdown_parser = mistune.create_markdown(renderer=mistune.AstRenderer()) blocks = markdown_parser(content) actual_type_filter = partial(type_filter, filter_) code_blocks = list(filter(block_code_filter, blocks)) bash_code_blocks = filter(actual_type_filter, code_blocks) return list(b["text"] for b in bash_code_blocks)
def find_headers(self, text): self._headers_found = [] markdown = mistune.create_markdown(renderer="ast") new_text, state = markdown.before_parse(text, {}) markdown.block.SETEX_HEADING = re.compile(r'([^\n]+)\n *(=|-){1,}[ \t]*\n+') markdown.block.parse_axt_heading = self.decorate_parse_axt_heading(markdown.block.parse_axt_heading) markdown.block.parse_setex_heading = self.decorate_parse_setex_heading(markdown.block.parse_setex_heading) markdown.block.parse(new_text, state) return new_text, self._headers_found
def setup_class(cls): """Test skill.md""" markdown_parser = mistune.create_markdown( renderer=mistune.AstRenderer()) skill_doc_file = Path(ROOT_DIR, "docs", "protocol.md") doc = markdown_parser(skill_doc_file.read_text()) # get only code blocks cls.code_blocks = list(filter(lambda x: x["type"] == "block_code", doc))
def test_before_parse_hooks(self): def _add_name(md, s, state): state['name'] = 'test' return s, state md = mistune.create_markdown() md.before_parse_hooks.append(_add_name) state = {} md.parse('', state) self.assertEqual(state['name'], 'test')
def custom_render_article(content): """ 渲染文章 :param content: Markdown内容 :return: 渲染后带样式的HTML内容 """ render = MyRenderer() content_result = mistune.create_markdown(renderer=render)(content) return content_result
def parse_markdown(content): body = content.long_description title = content.title date_created = content.date_created short_description = content.short_description # Calculate time since event in human readable format time_since = datetime.date.today() - date_created time_since = naturaldelta(time_since) plugins = ['strikethrough', 'footnotes', 'table', 'url', 'task_lists'] print('Parsing Markdown...') markdown = mistune.create_markdown( renderer=HighlightRenderer(escape=False), plugins=plugins) html = markdown(body) print(f'{Style.BRIGHT}{Fore.GREEN}(\u2713){Style.RESET_ALL} Markdown converted to html.') # Template variables template_vars = {'title': title, 'date_created': date_created.strftime("%B %d, %Y"), 'short_description': short_description, 'time_since': time_since} # Setup jinja template for markdown print('Rendering event description...') html = Template(html).render(template_vars) print(f'{Style.BRIGHT}{Fore.GREEN}(\u2713){Style.RESET_ALL} Rendered event description.') # Set up jinja template for main content template_vars['email_content'] = html template_file = 'email_template.html' with open(path_join(BASEDIR, 'templates', template_file)) as file: template = Template(file.read()) print('Enhancing html for better email content.') raw_html = template.render(template_vars) with open(path_join(BASEDIR, 'css', 'colorful.css')) as file: try: html = str(BeautifulSoup(transform(raw_html, remove_classes=True, css_text=file.read(), disable_validation=True), 'html.parser').prettify(formatter="html")) except Exception as e: print("Unable to add styles.") print('Exception: ' + str(e)) html_preview = path_join(BASEDIR, 'markdown', 'preview.html') with open(html_preview, 'w') as file: file.write(html) print(f'{Style.BRIGHT}{Fore.GREEN}(\u2713){Style.RESET_ALL} Uses inline' ' styling and syntax highlighting') print('HTML preview is available at: ') print(html_preview) return html
def test_extract_toc_items(self): md = create_markdown(renderer='ast', plugins=[DirectiveToc()]) self.assertEqual(extract_toc_items(md, ''), []) s = '# H1\n## H2\n# H1' result = extract_toc_items(md, s) expected = [ ('toc_1', 'H1', 1), ('toc_2', 'H2', 2), ('toc_3', 'H1', 1), ] self.assertEqual(result, expected)
def get_script(): doc = open('script.md', 'r').read() markdown = mistune.create_markdown(renderer=mistune.AstRenderer()) markdown_ast = markdown(doc) doc_ast = {} sections = [make_doc_section(s) for s in get_sections(markdown_ast)] return sections