From 22603bad891809b63e5d2be835af128a7879fd0b Mon Sep 17 00:00:00 2001 From: Quentin <14336407+Qrbaker@users.noreply.github.com> Date: Mon, 4 Jul 2022 13:59:09 -0400 Subject: [PATCH] Feature: Syntax Highlighting support (#316) --- static/truewiki/truewiki.css | 174 +++++++++++++++++++++++++++++++++++ truewiki/wiki_page.py | 8 ++ 2 files changed, 182 insertions(+) diff --git a/static/truewiki/truewiki.css b/static/truewiki/truewiki.css index e608ecc..37736ac 100644 --- a/static/truewiki/truewiki.css +++ b/static/truewiki/truewiki.css @@ -586,3 +586,177 @@ header { font-size: 18px; font-weight: bold; } + +/* Syntax Highlighting Light Mode - 'default' theme generated by Pygments (pygments.org/styles) */ +@media (prefers-color-scheme: light) { +.syntaxhighlighttable * { border:unset; padding:unset; color:unset; background-color:#f8f8f8; color:unset; padding:unset; box-sizing:unset; vertical-align:unset; } +.syntaxhighlighttable { border: 1px solid #dfdfdf;} + +pre { line-height: 125%; } +td.code { padding: 0 0.5em; width: 100%; } +td.linenos .normal { color: #bbbbbb; background-color: transparent; padding-left: 5px; padding-right: 5px; } +span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } +td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } +span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } +.syntaxhighlight .hll { background-color: #ffffcc } +.syntaxhighlight { background: #f8f8f8; } +.syntaxhighlight .c { color: #3D7B7B; font-style: italic } /* Comment */ +.syntaxhighlight .err { border: 1px solid #FF0000 } /* Error */ +.syntaxhighlight .k { color: #008000; font-weight: bold } /* Keyword */ +.syntaxhighlight .o { color: #666666 } /* Operator */ +.syntaxhighlight .ch { color: #3D7B7B; font-style: italic } /* Comment.Hashbang */ +.syntaxhighlight .cm { color: #3D7B7B; font-style: italic } /* Comment.Multiline */ +.syntaxhighlight .cp { color: #9C6500 } /* Comment.Preproc */ +.syntaxhighlight .cpf { color: #3D7B7B; font-style: italic } /* Comment.PreprocFile */ +.syntaxhighlight .c1 { color: #3D7B7B; font-style: italic } /* Comment.Single */ +.syntaxhighlight .cs { color: #3D7B7B; font-style: italic } /* Comment.Special */ +.syntaxhighlight .gd { color: #A00000 } /* Generic.Deleted */ +.syntaxhighlight .ge { font-style: italic } /* Generic.Emph */ +.syntaxhighlight .gr { color: #E40000 } /* Generic.Error */ +.syntaxhighlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */ +.syntaxhighlight .gi { color: #008400 } /* Generic.Inserted */ +.syntaxhighlight .go { color: #717171 } /* Generic.Output */ +.syntaxhighlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ +.syntaxhighlight .gs { font-weight: bold } /* Generic.Strong */ +.syntaxhighlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ +.syntaxhighlight .gt { color: #0044DD } /* Generic.Traceback */ +.syntaxhighlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ +.syntaxhighlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ +.syntaxhighlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ +.syntaxhighlight .kp { color: #008000 } /* Keyword.Pseudo */ +.syntaxhighlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ +.syntaxhighlight .kt { color: #B00040 } /* Keyword.Type */ +.syntaxhighlight .m { color: #666666 } /* Literal.Number */ +.syntaxhighlight .s { color: #BA2121 } /* Literal.String */ +.syntaxhighlight .na { color: #687822 } /* Name.Attribute */ +.syntaxhighlight .nb { color: #008000 } /* Name.Builtin */ +.syntaxhighlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */ +.syntaxhighlight .no { color: #880000 } /* Name.Constant */ +.syntaxhighlight .nd { color: #AA22FF } /* Name.Decorator */ +.syntaxhighlight .ni { color: #717171; font-weight: bold } /* Name.Entity */ +.syntaxhighlight .ne { color: #CB3F38; font-weight: bold } /* Name.Exception */ +.syntaxhighlight .nf { color: #0000FF } /* Name.Function */ +.syntaxhighlight .nl { color: #767600 } /* Name.Label */ +.syntaxhighlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ +.syntaxhighlight .nt { color: #008000; font-weight: bold } /* Name.Tag */ +.syntaxhighlight .nv { color: #19177C } /* Name.Variable */ +.syntaxhighlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ +.syntaxhighlight .w { color: #bbbbbb } /* Text.Whitespace */ +.syntaxhighlight .mb { color: #666666 } /* Literal.Number.Bin */ +.syntaxhighlight .mf { color: #666666 } /* Literal.Number.Float */ +.syntaxhighlight .mh { color: #666666 } /* Literal.Number.Hex */ +.syntaxhighlight .mi { color: #666666 } /* Literal.Number.Integer */ +.syntaxhighlight .mo { color: #666666 } /* Literal.Number.Oct */ +.syntaxhighlight .sa { color: #BA2121 } /* Literal.String.Affix */ +.syntaxhighlight .sb { color: #BA2121 } /* Literal.String.Backtick */ +.syntaxhighlight .sc { color: #BA2121 } /* Literal.String.Char */ +.syntaxhighlight .dl { color: #BA2121 } /* Literal.String.Delimiter */ +.syntaxhighlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ +.syntaxhighlight .s2 { color: #BA2121 } /* Literal.String.Double */ +.syntaxhighlight .se { color: #AA5D1F; font-weight: bold } /* Literal.String.Escape */ +.syntaxhighlight .sh { color: #BA2121 } /* Literal.String.Heredoc */ +.syntaxhighlight .si { color: #A45A77; font-weight: bold } /* Literal.String.Interpol */ +.syntaxhighlight .sx { color: #008000 } /* Literal.String.Other */ +.syntaxhighlight .sr { color: #A45A77 } /* Literal.String.Regex */ +.syntaxhighlight .s1 { color: #BA2121 } /* Literal.String.Single */ +.syntaxhighlight .ss { color: #19177C } /* Literal.String.Symbol */ +.syntaxhighlight .bp { color: #008000 } /* Name.Builtin.Pseudo */ +.syntaxhighlight .fm { color: #0000FF } /* Name.Function.Magic */ +.syntaxhighlight .vc { color: #19177C } /* Name.Variable.Class */ +.syntaxhighlight .vg { color: #19177C } /* Name.Variable.Global */ +.syntaxhighlight .vi { color: #19177C } /* Name.Variable.Instance */ +.syntaxhighlight .vm { color: #19177C } /* Name.Variable.Magic */ +.syntaxhighlight .il { color: #666666 } /* Literal.Number.Integer.Long */ +} + +/* Syntax Highlighting Dark Mode - 'dracula' theme generated by Pygments (pygments.org/styles) */ +@media (prefers-color-scheme: dark) { +.syntaxhighlighttable * { border:unset; padding:unset; color:unset; background-color:#263238; color:unset; padding:unset; box-sizing:unset; vertical-align:unset; } +.syntaxhighlighttable { border: 1px solid #dfdfdf;} + +pre { line-height: 125%; } +td.code { padding: 0 0.5em; width: 100%; } +td.linenos, td.linenos * { background-color: #4d6774;} +td.linenos .normal { color: #b9c6c6; background-color: #4d6774; padding: 0 0.3em 0 0.5em; } +span.linenos { color: #37474F; background-color: #263238; padding: 0 5px; } +td.linenos .special { color: #607A86; background-color: #263238; padding: 0 5px; } +span.linenos.special { color: #607A86; background-color: #263238; padding: 0 5px; } +.syntaxhighlight pre { background-color: transparent;} +.syntaxhighlight .hll { background-color: #2C3B41 } +.syntaxhighlight { background: #263238; color: #EEFFFF } +.syntaxhighlight .c { color: #546E7A; font-style: italic } /* Comment */ +.syntaxhighlight .err { color: #FF5370 } /* Error */ +.syntaxhighlight .esc { color: #89DDFF } /* Escape */ +.syntaxhighlight .g { color: #EEFFFF } /* Generic */ +.syntaxhighlight .k { color: #BB80B3 } /* Keyword */ +.syntaxhighlight .l { color: #C3E88D } /* Literal */ +.syntaxhighlight .n { color: #EEFFFF } /* Name */ +.syntaxhighlight .o { color: #89DDFF } /* Operator */ +.syntaxhighlight .p { color: #89DDFF } /* Punctuation */ +.syntaxhighlight .ch { color: #546E7A; font-style: italic } /* Comment.Hashbang */ +.syntaxhighlight .cm { color: #546E7A; font-style: italic } /* Comment.Multiline */ +.syntaxhighlight .cp { color: #546E7A; font-style: italic } /* Comment.Preproc */ +.syntaxhighlight .cpf { color: #546E7A; font-style: italic } /* Comment.PreprocFile */ +.syntaxhighlight .c1 { color: #546E7A; font-style: italic } /* Comment.Single */ +.syntaxhighlight .cs { color: #546E7A; font-style: italic } /* Comment.Special */ +.syntaxhighlight .gd { color: #FF5370 } /* Generic.Deleted */ +.syntaxhighlight .ge { color: #89DDFF } /* Generic.Emph */ +.syntaxhighlight .gr { color: #FF5370 } /* Generic.Error */ +.syntaxhighlight .gh { color: #C3E88D } /* Generic.Heading */ +.syntaxhighlight .gi { color: #C3E88D } /* Generic.Inserted */ +.syntaxhighlight .go { color: #546E7A } /* Generic.Output */ +.syntaxhighlight .gp { color: #FFCB6B } /* Generic.Prompt */ +.syntaxhighlight .gs { color: #FF5370 } /* Generic.Strong */ +.syntaxhighlight .gu { color: #89DDFF } /* Generic.Subheading */ +.syntaxhighlight .gt { color: #FF5370 } /* Generic.Traceback */ +.syntaxhighlight .kc { color: #89DDFF } /* Keyword.Constant */ +.syntaxhighlight .kd { color: #BB80B3 } /* Keyword.Declaration */ +.syntaxhighlight .kn { color: #89DDFF; font-style: italic } /* Keyword.Namespace */ +.syntaxhighlight .kp { color: #89DDFF } /* Keyword.Pseudo */ +.syntaxhighlight .kr { color: #BB80B3 } /* Keyword.Reserved */ +.syntaxhighlight .kt { color: #BB80B3 } /* Keyword.Type */ +.syntaxhighlight .ld { color: #C3E88D } /* Literal.Date */ +.syntaxhighlight .m { color: #F78C6C } /* Literal.Number */ +.syntaxhighlight .s { color: #C3E88D } /* Literal.String */ +.syntaxhighlight .na { color: #BB80B3 } /* Name.Attribute */ +.syntaxhighlight .nb { color: #82AAFF } /* Name.Builtin */ +.syntaxhighlight .nc { color: #FFCB6B } /* Name.Class */ +.syntaxhighlight .no { color: #EEFFFF } /* Name.Constant */ +.syntaxhighlight .nd { color: #82AAFF } /* Name.Decorator */ +.syntaxhighlight .ni { color: #89DDFF } /* Name.Entity */ +.syntaxhighlight .ne { color: #FFCB6B } /* Name.Exception */ +.syntaxhighlight .nf { color: #82AAFF } /* Name.Function */ +.syntaxhighlight .nl { color: #82AAFF } /* Name.Label */ +.syntaxhighlight .nn { color: #FFCB6B } /* Name.Namespace */ +.syntaxhighlight .nx { color: #EEFFFF } /* Name.Other */ +.syntaxhighlight .py { color: #FFCB6B } /* Name.Property */ +.syntaxhighlight .nt { color: #FF5370 } /* Name.Tag */ +.syntaxhighlight .nv { color: #89DDFF } /* Name.Variable */ +.syntaxhighlight .ow { color: #89DDFF; font-style: italic } /* Operator.Word */ +.syntaxhighlight .w { color: #EEFFFF } /* Text.Whitespace */ +.syntaxhighlight .mb { color: #F78C6C } /* Literal.Number.Bin */ +.syntaxhighlight .mf { color: #F78C6C } /* Literal.Number.Float */ +.syntaxhighlight .mh { color: #F78C6C } /* Literal.Number.Hex */ +.syntaxhighlight .mi { color: #F78C6C } /* Literal.Number.Integer */ +.syntaxhighlight .mo { color: #F78C6C } /* Literal.Number.Oct */ +.syntaxhighlight .sa { color: #BB80B3 } /* Literal.String.Affix */ +.syntaxhighlight .sb { color: #C3E88D } /* Literal.String.Backtick */ +.syntaxhighlight .sc { color: #C3E88D } /* Literal.String.Char */ +.syntaxhighlight .dl { color: #EEFFFF } /* Literal.String.Delimiter */ +.syntaxhighlight .sd { color: #546E7A; font-style: italic } /* Literal.String.Doc */ +.syntaxhighlight .s2 { color: #C3E88D } /* Literal.String.Double */ +.syntaxhighlight .se { color: #EEFFFF } /* Literal.String.Escape */ +.syntaxhighlight .sh { color: #C3E88D } /* Literal.String.Heredoc */ +.syntaxhighlight .si { color: #89DDFF } /* Literal.String.Interpol */ +.syntaxhighlight .sx { color: #C3E88D } /* Literal.String.Other */ +.syntaxhighlight .sr { color: #89DDFF } /* Literal.String.Regex */ +.syntaxhighlight .s1 { color: #C3E88D } /* Literal.String.Single */ +.syntaxhighlight .ss { color: #89DDFF } /* Literal.String.Symbol */ +.syntaxhighlight .bp { color: #89DDFF } /* Name.Builtin.Pseudo */ +.syntaxhighlight .fm { color: #82AAFF } /* Name.Function.Magic */ +.syntaxhighlight .vc { color: #89DDFF } /* Name.Variable.Class */ +.syntaxhighlight .vg { color: #89DDFF } /* Name.Variable.Global */ +.syntaxhighlight .vi { color: #89DDFF } /* Name.Variable.Instance */ +.syntaxhighlight .vm { color: #82AAFF } /* Name.Variable.Magic */ +.syntaxhighlight .il { color: #F78C6C } /* Literal.Number.Integer.Long */ +} diff --git a/truewiki/wiki_page.py b/truewiki/wiki_page.py index bb30e62..fc786ec 100644 --- a/truewiki/wiki_page.py +++ b/truewiki/wiki_page.py @@ -79,6 +79,14 @@ def __init__(self, page: str) -> None: self.en_page = None self.categories = [] + # Overrides wikitexthtml methods so syntax highlighting is enabled everywhere. + # We need the throwaway argument to match # of args in superclass. Otherwise, we get a TypeError. + def prepare(self, body, _=None): + return super().prepare(body, True) + + def render(self, body: str = None, _=None): + return super().render(body, True) + def page_load(self, page: str) -> str: namespace = page.split("/")[0] return NAMESPACES.get(namespace, NAMESPACE_DEFAULT_PAGE).page_load(page)