【问题标题】:How do I prevent Kramdown from getting confused by a C++ include?如何防止 Kramdown 被 C++ 包含混淆?
【发布时间】:2015-05-10 12:18:09
【问题描述】:

我写了这个插件:

module Jekyll
    module Tags
        class Prism < Liquid::Block
            def initialize(tag_name, text, tokens)
                @arg = text.strip
                super
            end

            def render(context)
                output = super(context)
                "<pre><code class=\"language-#{@arg}\">#{output}</code></pre>"
            end
        end
    end
end 

Liquid::Template.register_tag('prism', Jekyll::Tags::Prism)

我就是这样使用它的:

{% prism cpp %}
#include <iostream>

// Hello World
int main()
{
    cout << "hello world" << endl;
    int a = 10;
}
{% endprism %}

现在,问题是,我主要在我的网站上使用 C++ 代码。当我现在使用 Jekyll 生成此降价时,{% endprism %} 之后的所有文本仍将在 &lt;pre&gt; 标签内,因为 Kramdown 被 &lt;iostream&gt; 弄糊涂了正如预期的那样,但我的 Javascript Highlighter 变得混乱了。

如果不启用 Jekyll 的荧光笔,我该如何解决这种情况?

【问题讨论】:

    标签: jekyll kramdown


    【解决方案1】:

    我认为您尝试使用 GitHub Flavored Markdown 中使用的防护代码块。

    您为什么不使用 Jekyll code highlightingcpp 开箱即用地完美工作?高亮的基本 css 可用here

    试试:

    {% highlight cpp %}
    #include <iostream>
    
    // Hello World
    int main()
    {
        cout << "hello world" << endl;
        int a = 10;
    }
    {% endhighlight %}
    

    【讨论】:

    • 因为我想用prism.js。当 jekyll 解析这个时,kramdown 如何不感到困惑?它是一样的,只是一个不同的markdown标签......突出显示而不是棱镜。另外,学习插件的东西。
    【解决方案2】:

    CGI 模块中有一个函数,它允许转义 HTML,就像 PHP 中的 htmlspecialchars 一样。我将液体插件更改为这个并且它可以工作:

    require 'cgi'
    
    module Jekyll
        module Tags     
            class Prism < Liquid::Block
                def initialize(tag_name, text, tokens)
                    @arg = text.strip
                    super
                end
    
                def render(context)
                    output = super(context)
                    output = CGI.escapeHTML(output);
                    "<pre><code class=\"language-#{@arg}\">#{output}</code></pre>"
                end
            end
        end
    end 
    
    Liquid::Template.register_tag('prism', Jekyll::Tags::Prism)
    

    它将所有 转义为 html 特殊字符。因此,Kramdown 不会感到困惑,prism.js 仍然能够正确突出显示代码。

    【讨论】:

      猜你喜欢
      • 2018-05-10
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2015-04-02
      • 1970-01-01
      相关资源
      最近更新 更多