def markdownify(text): return markdown(text, extensions=[ 'markdown.extensions.extra', 'markdown.extensions.nl2br', # turn newlines into breaks 'markdown.extensions.sane_lists', LinkifyExtension(), # pass class for pyinstaller to bundle ])
def getparser(): # Return a correctly configured markdown parser linkify_configs = {'linkifycallbacks': [[dont_linkify_python], '']} linkify = LinkifyExtension(configs=linkify_configs) markdown_extensions = ['extra', 'codehilite', 'sane_lists', linkify] return markdown.Markdown(extensions=markdown_extensions)
def test_callbacks(self): def dont_linkify_python(attrs, new=False): if not new: # This is an existing <a> tag, leave it be. return attrs # If the TLD is '.py', make sure it starts with http: or https: text = attrs['_text'] if text.endswith('.py') and \ not text.startswith(('www.', 'http:', 'https:')): # This looks like a Python file, not a URL. Don't make a link. return None # Everything checks out, keep going to the next callback. return attrs configs = { 'linkifycallbacks': [[dont_linkify_python], ''] } linkify_ext = LinkifyExtension(configs=configs) md = Markdown(extensions=[linkify_ext]) text = "setup.com www.setup.py http://setup.py setup.py" expected = ('<p><a href="http://setup.com">setup.com</a> ' '<a href="http://www.setup.py">www.setup.py</a> ' '<a href="http://setup.py">http://setup.py</a> ' 'setup.py</p>') actual = md.convert(text) self.assertEqual(expected, actual)
def test_callbacks(self): def dont_linkify_net_extension(attrs, new=False): if attrs["_text"].endswith(".net"): return None return attrs # assert expected behavior WITHOUT our callback actual = markdown("https://linked.net", extensions=["mdx_linkify"]) expected = '<p><a href="https://linked.net">https://linked.net</a></p>' self.assertEqual(actual, expected) md = Markdown(extensions=[ LinkifyExtension(linkify_callbacks=[dont_linkify_net_extension]) ], ) # assert .net no longer works actual = md.convert("https://not-linked.net") expected = '<p>https://not-linked.net</p>' self.assertEqual(actual, expected) # assert other links still work actual = md.convert("example.com") expected = '<p><a href="http://example.com">example.com</a></p>' self.assertEqual(actual, expected) # assert that configuration parameters can be over-ridden at run time # https://python-markdown.github.io/extensions/api/#configsettings expected = '<p><a href="https://not-linked.net">https://not-linked.net</a></p>' actual = markdown("https://not-linked.net", extensions=["mdx_linkify"]) self.assertEqual(expected, actual)
def create_app(): global application if not application: application = Gthnk() application.facet("configuration") application.facet("logs") application.facet("database") application.facet("marshalling") application.facet("blueprints") application.facet("accounts") application.facet("signals") application.facet("forms") application.facet("error_handlers") application.facet("request_handlers") application.facet("administration") # application.facet("rest", api_map=api_map) # application.facet("webassets") # application.facet("email") # application.facet("debugger") # application.facet("task_queue") application.app.permanent_session_lifetime = timedelta(minutes=30) application.app.logger.info("starting gthnk server") application.app.md = Markdown( application.app, extensions=[LinkifyExtension(), JournalExtension()]) # print(application.app.url_map) return (application.app)
def test_custom_url_re(self): url_re = build_url_re(["example"]) expected = '<p><a href="https://domain.example" rel="nofollow">https://domain.example</a></p>' actual = markdown( "https://domain.example", extensions=[LinkifyExtension(linker_options={"url_re": url_re})], ) self.assertEqual(expected, actual)
def markdownify(text, linkify=True): extensions = [ 'markdown.extensions.extra', 'markdown.extensions.nl2br', # turn newlines into breaks 'markdown.extensions.sane_lists', ] if linkify: extensions.append(LinkifyExtension()) return markdown(text=text, extensions=extensions)
def init_app(app): linkify = LinkifyExtension(configs={'linkify_callbacks': [set_nofollow]}) app.extensions['markdown'] = UDataMarkdown(app, extensions=[linkify]) @app.template_filter() def mdstrip(value, length=None): ''' Truncate and strip tags from a markdown source The markdown source is truncated at the excerpt if present and smaller than the required length. Then, all html tags are stripped. ''' if not value: return '' if EXCERPT_TOKEN in value: value = value.split(EXCERPT_TOKEN, 1)[0] if length > 0: value = do_truncate(value, length) rendered = md(value) return do_striptags(rendered)
def test_callbacks(self): def dont_linkify_txt_extension(attrs, new=False): if attrs["_text"].endswith(".txt"): return None return attrs md = Markdown(extensions=[LinkifyExtension()], extension_configs={ "linkify": { "linkify_callbacks": [[dont_linkify_txt_extension], ""] } }) actual = md.convert("not_link.txt") expected = '<p>not_link.txt</p>' self.assertEqual(actual, expected) actual = md.convert("example.com") expected = '<p><a href="http://example.com">example.com</a></p>' self.assertEqual(actual, expected)
def create_app(): app = flask.Flask(__name__) try: app.config.from_envvar('SETTINGS') except RuntimeError: default_filename = os.path.expanduser('~/.gthnk/gthnk.conf') if os.path.isfile(default_filename): print("WARN: using default configuration file ~/.gthnk/gthnk.conf") app.config.from_pyfile(default_filename) logging.basicConfig( format='%(asctime)s %(module)-16s %(levelname)-8s %(message)s', filename=app.config["LOG"], level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S') logging.info("Server: Start") logging.info("Database: {}".format(app.config['SQLALCHEMY_DATABASE_URI'])) from .blueprints.root import root app.register_blueprint(root) from .blueprints.auth import auth app.register_blueprint(auth) from .blueprints.day import day app.register_blueprint(day) # from .blueprints.attachments import attachments # app.register_blueprint(attachments) db.init_app(app) login_manager.init_app(app) bcrypt.init_app(app) app.markdown = Markdown( app, extensions=[LinkifyExtension(), JournalExtension()]) return app
def test_no_email(self): expected = '<p>[email protected]</p>' actual = markdown("*****@*****.**", extensions=[ LinkifyExtension(linker_options={"parse_email": False}), ]) self.assertEqual(expected, actual)
def test_email(self): expected = '<p><a href="mailto:[email protected]">[email protected]</a></p>' actual = markdown("*****@*****.**", extensions=[ LinkifyExtension(linker_options={"parse_email": True}), ]) self.assertEqual(expected, actual)
except ImportError as e: print(e) try: from mdx_superscript import SuperscriptExtension extensions_list.append(SuperscriptExtension()) except ImportError as e: print(e) try: from mdx_subscript import SubscriptExtension extensions_list.append(SubscriptExtension()) except ImportError as e: print(e) try: from mdx_linkify.mdx_linkify import LinkifyExtension extensions_list.append(LinkifyExtension()) except ImportError as e: print(e) tags = ALLOWED_TAGS + [ "div", "i", "dl", "dt", "dd", "sup", "sub", "table", "th", "tr", "tbody", "td", "ul", "ol", "li" ] ALLOWED_ATTRIBUTES.update({"div": ['class="toc"']}) bleach = BleachExtension(tags=tags, ) #extensions_list.append(bleach) # Disable bleach extension because it's broken md = markdown.Markdown(extensions=extensions_list)