mirror of
https://github.com/zulip/zulip.git
synced 2025-11-03 05:23:35 +00:00
bugdown: Use Markdown.reset() instead of re-creating the whole parser
(imported from commit 45a65453f2178a6a73392e2bd1e7d6d03de0e0e7)
This commit is contained in:
@@ -53,12 +53,15 @@ class Bugdown(markdown.Extension):
|
|||||||
link_regex = r'\b(?P<url>https?://[^\s]+?)(?=[^\w/]*(\s|\Z))'
|
link_regex = r'\b(?P<url>https?://[^\s]+?)(?=[^\w/]*(\s|\Z))'
|
||||||
md.inlinePatterns.add('autolink', AutoLink(link_regex), '>link')
|
md.inlinePatterns.add('autolink', AutoLink(link_regex), '>link')
|
||||||
|
|
||||||
# We need to re-initialize the markdown engine every 30 messages
|
_md_engine = markdown.Markdown(
|
||||||
# due to some sort of performance leak in the markdown library.
|
safe_mode = 'escape',
|
||||||
MAX_MD_ENGINE_USES = 30
|
output_format = 'html',
|
||||||
|
extensions = ['nl2br',
|
||||||
_md_engine = None
|
codehilite.makeExtension(configs=[
|
||||||
_use_count = 0
|
('force_linenos', False),
|
||||||
|
('guess_lang', False)]),
|
||||||
|
fenced_code.makeExtension(),
|
||||||
|
Bugdown()])
|
||||||
|
|
||||||
# We want to log Markdown parser failures, but shouldn't log the actual input
|
# We want to log Markdown parser failures, but shouldn't log the actual input
|
||||||
# message for privacy reasons. The compromise is to replace all alphanumeric
|
# message for privacy reasons. The compromise is to replace all alphanumeric
|
||||||
@@ -76,18 +79,9 @@ def _linkify(match):
|
|||||||
|
|
||||||
def convert(md):
|
def convert(md):
|
||||||
"""Convert Markdown to HTML, with Humbug-specific settings and hacks."""
|
"""Convert Markdown to HTML, with Humbug-specific settings and hacks."""
|
||||||
global _md_engine, _use_count
|
|
||||||
|
|
||||||
if _md_engine is None:
|
# Reset the parser; otherwise it will get slower over time.
|
||||||
_md_engine = markdown.Markdown(
|
_md_engine.reset()
|
||||||
safe_mode = 'escape',
|
|
||||||
output_format = 'html',
|
|
||||||
extensions = ['nl2br',
|
|
||||||
codehilite.makeExtension(configs=[
|
|
||||||
('force_linenos', False),
|
|
||||||
('guess_lang', False)]),
|
|
||||||
fenced_code.makeExtension(),
|
|
||||||
Bugdown()])
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
html = _md_engine.convert(md)
|
html = _md_engine.convert(md)
|
||||||
@@ -97,9 +91,4 @@ def convert(md):
|
|||||||
logging.getLogger('').error('Exception in Markdown parser: %sInput (sanitized) was: %s'
|
logging.getLogger('').error('Exception in Markdown parser: %sInput (sanitized) was: %s'
|
||||||
% (traceback.format_exc(), _sanitize_for_log(md)))
|
% (traceback.format_exc(), _sanitize_for_log(md)))
|
||||||
|
|
||||||
_use_count += 1
|
|
||||||
if _use_count >= MAX_MD_ENGINE_USES:
|
|
||||||
_md_engine = None
|
|
||||||
_use_count = 0
|
|
||||||
|
|
||||||
return html
|
return html
|
||||||
|
|||||||
Reference in New Issue
Block a user