diff --git a/layout/page.html b/layout/page.html index eec5c16..61a6bc1 100644 --- a/layout/page.html +++ b/layout/page.html @@ -9,6 +9,7 @@ + diff --git a/makesite.py b/makesite.py index 667d951..2bc12b2 100755 --- a/makesite.py +++ b/makesite.py @@ -40,10 +40,61 @@ import unicodedata import locale import requests import mistune +from pygments import highlight +from pygments.lexers import get_lexer_by_name +from pygments.formatters import html # set user locale locale.setlocale(locale.LC_ALL, "") +# initialize markdown +def block_code(text, lang, inlinestyles=False, linenos=False): + if not lang: + text = text.strip() + return u'
%s
\n' % mistune.escape(text) + + try: + lexer = get_lexer_by_name(lang, stripall=True) + formatter = HtmlFormatter( + noclasses=inlinestyles, linenos=linenos + ) + code = highlight(text, lexer, formatter) + if linenos: + return '
%s
\n' % code + return code + except: + return '
%s
\n' % ( + lang, mistune.escape(text) + ) + + +class HighlightMixin(object): + + options = {'escape': False, 'hard_wrap':True} + + def block_code(self, text, lang): + # renderer has an options + inlinestyles = self.options.get('inlinestyles', False) + linenos = self.options.get('linenos', False) + return block_code(text, lang, inlinestyles, linenos) + + +class HighlightRenderer(mistune.Renderer): + + options = {'escape': False, 'hard_wrap':True} + + def block_code(self, code, lang): + if not lang: + return '\n
%s
\n' % \ + mistune.escape(code) + lexer = get_lexer_by_name(lang, stripall=True) + formatter = html.HtmlFormatter() + return highlight(code, lexer, formatter) + +#markdown_renderer = mistune.Renderer(escape=False, hard_wrap=True) +#markdown = mistune.Markdown(renderer=HighlightMixin) +renderer = HighlightRenderer() +markdown = mistune.Markdown(renderer=renderer) def fread(filename): """Read file and close the file.""" @@ -124,7 +175,7 @@ def read_content(filename): # Convert Markdown content to HTML. if filename.endswith((".md", ".mkd", ".mkdn", ".mdown", ".markdown")): clean_text = text.replace('', '') - text = mistune.markdown(clean_text) + text = markdown(clean_text) # Update the dictionary with content and RFC 2822 date. content.update({"content": text, "rfc_2822_date": rfc_2822_format(content["date"])}) @@ -235,7 +286,7 @@ def make_posts( avatar=comment.get("avatar", ""), site=comment.get("site", ""), date=comment["date"], - content=mistune.markdown(comment["content"]), + content=markdown(comment["content"]), ) out_comments.append(out_comment) page_params["comments"] = "".join(out_comments) diff --git a/posts/2017/2017-07-24-golang-a-la-rescousse.md b/posts/2017/2017-07-24-golang-a-la-rescousse.md index 15c925e..af55ec0 100755 --- a/posts/2017/2017-07-24-golang-a-la-rescousse.md +++ b/posts/2017/2017-07-24-golang-a-la-rescousse.md @@ -71,7 +71,7 @@ Golang pour gérer la concurrence de traitement. Pour les fans de code, voici celui du serveur HTTP avec cache : -``` golang +``` go package main import ( diff --git a/requirements.txt b/requirements.txt index a6f708e..ed3ea3a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,6 +7,7 @@ Click==7.0 future==0.17.1 idna==2.8 mistune==0.8.4 +Pygments==2.4.2 requests==2.22.0 toml==0.10.0 urllib3==1.25.3 diff --git a/static/css/pygments-default.css b/static/css/pygments-default.css new file mode 100644 index 0000000..75a4308 --- /dev/null +++ b/static/css/pygments-default.css @@ -0,0 +1,62 @@ +.highlight .hll { background-color: #ffffcc } +.highlight .c { color: #408080; font-style: italic } /* Comment */ +.highlight .err { border: 1px solid #FF0000 } /* Error */ +.highlight .k { color: #008000; font-weight: bold } /* Keyword */ +.highlight .o { color: #666666 } /* Operator */ +.highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */ +.highlight .cp { color: #BC7A00 } /* Comment.Preproc */ +.highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */ +.highlight .cs { color: #408080; font-style: italic } /* Comment.Special */ +.highlight .gd { color: #A00000 } /* Generic.Deleted */ +.highlight .ge { font-style: italic } /* Generic.Emph */ +.highlight .gr { color: #FF0000 } /* Generic.Error */ +.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */ +.highlight .gi { color: #00A000 } /* Generic.Inserted */ +.highlight .go { color: #808080 } /* Generic.Output */ +.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */ +.highlight .gs { font-weight: bold } /* Generic.Strong */ +.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */ +.highlight .gt { color: #0040D0 } /* Generic.Traceback */ +.highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */ +.highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */ +.highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */ +.highlight .kp { color: #008000 } /* Keyword.Pseudo */ +.highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */ +.highlight .kt { color: #B00040 } /* Keyword.Type */ +.highlight .m { color: #666666 } /* Literal.Number */ +.highlight .s { color: #BA2121 } /* Literal.String */ +.highlight .na { color: #7D9029 } /* Name.Attribute */ +.highlight .nb { color: #008000 } /* Name.Builtin */ +.highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */ +.highlight .no { color: #880000 } /* Name.Constant */ +.highlight .nd { color: #AA22FF } /* Name.Decorator */ +.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */ +.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */ +.highlight .nf { color: #0000FF } /* Name.Function */ +.highlight .nl { color: #A0A000 } /* Name.Label */ +.highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */ +.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */ +.highlight .nv { color: #19177C } /* Name.Variable */ +.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */ +.highlight .w { color: #bbbbbb } /* Text.Whitespace */ +.highlight .mf { color: #666666 } /* Literal.Number.Float */ +.highlight .mh { color: #666666 } /* Literal.Number.Hex */ +.highlight .mi { color: #666666 } /* Literal.Number.Integer */ +.highlight .mo { color: #666666 } /* Literal.Number.Oct */ +.highlight .sb { color: #BA2121 } /* Literal.String.Backtick */ +.highlight .sc { color: #BA2121 } /* Literal.String.Char */ +.highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */ +.highlight .s2 { color: #BA2121 } /* Literal.String.Double */ +.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */ +.highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */ +.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */ +.highlight .sx { color: #008000 } /* Literal.String.Other */ +.highlight .sr { color: #BB6688 } /* Literal.String.Regex */ +.highlight .s1 { color: #BA2121 } /* Literal.String.Single */ +.highlight .ss { color: #19177C } /* Literal.String.Symbol */ +.highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */ +.highlight .vc { color: #19177C } /* Name.Variable.Class */ +.highlight .vg { color: #19177C } /* Name.Variable.Global */ +.highlight .vi { color: #19177C } /* Name.Variable.Instance */ +.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */ +