From 9312bda4eb709038ccd4cff8a5676ca28571dcca Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 21 2016 09:27:02 +0000 Subject: [PATCH 1/4] Enable some of the markdown extensions by default Fixes https://pagure.io/pagure/issue/1455 --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 278fed7..fea2cea 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2891,10 +2891,19 @@ def add_token_to_user(session, project, acls, username): def text2markdown(text, extended=True): """ Simple text to html converter using the markdown library. """ - md_processor = markdown.Markdown(safe_mode="escape") + extensions = [ + 'markdown.extensions.extra', + 'markdown.extensions.nl2br', + 'markdown.extensions.toc', + ] if extended: # Install our markdown modifications - md_processor = markdown.Markdown(extensions=['pagure.pfmarkdown']) + extensions.append('pagure.pfmarkdown') + + md_processor = markdown.Markdown( + safe_mode="escape", + extensions=extensions, + ) if text: # Hack to allow blockquotes to be marked by ~~~ @@ -2937,9 +2946,9 @@ def clean_input(text, ignore=None): tags = bleach.ALLOWED_TAGS + [ 'p', 'br', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', - 'table', 'td', 'tr', 'th', - 'col', 'tbody', 'pre', 'img', 'hr', 'dl', 'dt', 'dd', 'span', - 'kbd', 'var', 'del', + 'table', 'td', 'tr', 'th', 'thead','tbody', + 'col', 'pre', 'img', 'hr', 'dl', 'dt', 'dd', 'span', + 'kbd', 'var', 'del', 'cite', ] if ignore: for tag in ignore: From 9b0a29f8e3dd5f370e7bd1f9cbbe14b790c9e7e0 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 21 2016 09:27:02 +0000 Subject: [PATCH 2/4] Enable a few more markdowns extensions including code highlighting This codehilite extensions requires the CSS change made here. The CSS was generated with the command: pygmentize -S tango -f html -a .codehilite >> pagure/static/pagure.css (hopefully it will work in epel7 as well) --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index fea2cea..fbbf808 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2893,7 +2893,10 @@ def text2markdown(text, extended=True): """ extensions = [ 'markdown.extensions.extra', + 'markdown.extensions.admonition', + 'markdown.extensions.codehilite', 'markdown.extensions.nl2br', + 'markdown.extensions.sane_lists', 'markdown.extensions.toc', ] if extended: @@ -2941,6 +2944,8 @@ def clean_input(text, ignore=None): attrs = bleach.ALLOWED_ATTRIBUTES.copy() attrs['table'] = ['class'] + attrs['span'] = ['class', 'id'] + attrs['div'] = ['class'] if not ignore or not 'img' in ignore: attrs['img'] = filter_img_src diff --git a/pagure/static/pagure.css b/pagure/static/pagure.css index 3f56485..721379e 100644 --- a/pagure/static/pagure.css +++ b/pagure/static/pagure.css @@ -593,3 +593,77 @@ a.nav-link.btn{ */ line-height: 14px; } + +.codehilite .hll { background-color: #ffffcc } +.codehilite { background: #f8f8f8; } +.codehilite .c { color: #8f5902; font-style: italic } /* Comment */ +.codehilite .err { color: #a40000; border: 1px solid #ef2929 } /* Error */ +.codehilite .g { color: #000000 } /* Generic */ +.codehilite .k { color: #204a87; font-weight: bold } /* Keyword */ +.codehilite .l { color: #000000 } /* Literal */ +.codehilite .n { color: #000000 } /* Name */ +.codehilite .o { color: #ce5c00; font-weight: bold } /* Operator */ +.codehilite .x { color: #000000 } /* Other */ +.codehilite .p { color: #000000; font-weight: bold } /* Punctuation */ +.codehilite .ch { color: #8f5902; font-style: italic } /* Comment.Hashbang */ +.codehilite .cm { color: #8f5902; font-style: italic } /* Comment.Multiline */ +.codehilite .cp { color: #8f5902; font-style: italic } /* Comment.Preproc */ +.codehilite .cpf { color: #8f5902; font-style: italic } /* Comment.PreprocFile */ +.codehilite .c1 { color: #8f5902; font-style: italic } /* Comment.Single */ +.codehilite .cs { color: #8f5902; font-style: italic } /* Comment.Special */ +.codehilite .gd { color: #a40000 } /* Generic.Deleted */ +.codehilite .ge { color: #000000; font-style: italic } /* Generic.Emph */ +.codehilite .gr { color: #ef2929 } /* Generic.Error */ +.codehilite .gh { color: #000080; font-weight: bold } /* Generic.Heading */ +.codehilite .gi { color: #00A000 } /* Generic.Inserted */ +.codehilite .go { color: #000000; font-style: italic } /* Generic.Output */ +.codehilite .gp { color: #8f5902 } /* Generic.Prompt */ +.codehilite .gs { color: #000000; font-weight: bold } /* Generic.Strong */ +.codehilite .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ +.codehilite .gt { color: #a40000; font-weight: bold } /* Generic.Traceback */ +.codehilite .kc { color: #204a87; font-weight: bold } /* Keyword.Constant */ +.codehilite .kd { color: #204a87; font-weight: bold } /* Keyword.Declaration */ +.codehilite .kn { color: #204a87; font-weight: bold } /* Keyword.Namespace */ +.codehilite .kp { color: #204a87; font-weight: bold } /* Keyword.Pseudo */ +.codehilite .kr { color: #204a87; font-weight: bold } /* Keyword.Reserved */ +.codehilite .kt { color: #204a87; font-weight: bold } /* Keyword.Type */ +.codehilite .ld { color: #000000 } /* Literal.Date */ +.codehilite .m { color: #0000cf; font-weight: bold } /* Literal.Number */ +.codehilite .s { color: #4e9a06 } /* Literal.String */ +.codehilite .na { color: #c4a000 } /* Name.Attribute */ +.codehilite .nb { color: #204a87 } /* Name.Builtin */ +.codehilite .nc { color: #000000 } /* Name.Class */ +.codehilite .no { color: #000000 } /* Name.Constant */ +.codehilite .nd { color: #5c35cc; font-weight: bold } /* Name.Decorator */ +.codehilite .ni { color: #ce5c00 } /* Name.Entity */ +.codehilite .ne { color: #cc0000; font-weight: bold } /* Name.Exception */ +.codehilite .nf { color: #000000 } /* Name.Function */ +.codehilite .nl { color: #f57900 } /* Name.Label */ +.codehilite .nn { color: #000000 } /* Name.Namespace */ +.codehilite .nx { color: #000000 } /* Name.Other */ +.codehilite .py { color: #000000 } /* Name.Property */ +.codehilite .nt { color: #204a87; font-weight: bold } /* Name.Tag */ +.codehilite .nv { color: #000000 } /* Name.Variable */ +.codehilite .ow { color: #204a87; font-weight: bold } /* Operator.Word */ +.codehilite .w { color: #f8f8f8; text-decoration: underline } /* Text.Whitespace */ +.codehilite .mb { color: #0000cf; font-weight: bold } /* Literal.Number.Bin */ +.codehilite .mf { color: #0000cf; font-weight: bold } /* Literal.Number.Float */ +.codehilite .mh { color: #0000cf; font-weight: bold } /* Literal.Number.Hex */ +.codehilite .mi { color: #0000cf; font-weight: bold } /* Literal.Number.Integer */ +.codehilite .mo { color: #0000cf; font-weight: bold } /* Literal.Number.Oct */ +.codehilite .sb { color: #4e9a06 } /* Literal.String.Backtick */ +.codehilite .sc { color: #4e9a06 } /* Literal.String.Char */ +.codehilite .sd { color: #8f5902; font-style: italic } /* Literal.String.Doc */ +.codehilite .s2 { color: #4e9a06 } /* Literal.String.Double */ +.codehilite .se { color: #4e9a06 } /* Literal.String.Escape */ +.codehilite .sh { color: #4e9a06 } /* Literal.String.Heredoc */ +.codehilite .si { color: #4e9a06 } /* Literal.String.Interpol */ +.codehilite .sx { color: #4e9a06 } /* Literal.String.Other */ +.codehilite .sr { color: #4e9a06 } /* Literal.String.Regex */ +.codehilite .s1 { color: #4e9a06 } /* Literal.String.Single */ +.codehilite .ss { color: #4e9a06 } /* Literal.String.Symbol */ +.codehilite .bp { color: #3465a4 } /* Name.Builtin.Pseudo */ +.codehilite .vc { color: #000000 } /* Name.Variable.Class */ +.codehilite .vg { color: #000000 } /* Name.Variable.Global */ +.codehilite .vi { color: #000000 } /* Name.Variable.Instance */ +.codehilite .il { color: #0000cf; font-weight: bold } /* Literal.Number.Integer.Long */ From 5163994b19197eef2fc6f46e644ef976980a6f4c Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 21 2016 09:27:02 +0000 Subject: [PATCH 3/4] With the markdown extensions, no need to hack anything anymore --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index fbbf808..26aa32d 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2909,17 +2909,7 @@ def text2markdown(text, extended=True): ) if text: - # Hack to allow blockquotes to be marked by ~~~ - ntext = [] - indent = False - for line in text.split('\n'): - if line.startswith('~~~'): - indent = not indent - continue - if indent: - line = ' %s' % line - ntext.append(line) - return clean_input(md_processor.convert('\n'.join(ntext))) + return clean_input(md_processor.convert(text)) return '' From f3ee383e52a6c878f0fdb7a6ea30efd7e5829ce7 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 21 2016 09:54:54 +0000 Subject: [PATCH 4/4] Drop the extra extension to drop the attr_lists one which we do *not* want --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 26aa32d..1d1bd37 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -2892,7 +2892,13 @@ def text2markdown(text, extended=True): """ Simple text to html converter using the markdown library. """ extensions = [ - 'markdown.extensions.extra', + 'markdown.extensions.abbr', + 'markdown.extensions.def_list', + 'markdown.extensions.fenced_code', + 'markdown.extensions.footnotes', + 'markdown.extensions.tables', + 'markdown.extensions.smart_strong', + # All of the above are the .extra extensions - the attribute lists one 'markdown.extensions.admonition', 'markdown.extensions.codehilite', 'markdown.extensions.nl2br',