Geshi 与 Markdown

发布于 2024-10-03 17:18:36 字数 977 浏览 0 评论 0原文

我正在尝试让 GeSHi 与 Markdown 一起使用。

Geshi 的一个简单用法如下:

$geshi = new GeSHi($message, 'c');
print $geshi->parse_code();

上面的代码接收整个消息并将其转换为突出显示的代码

我也有我的 Markdown 函数

print Markdown($message);

我试图使用回调函数来 preg_match

 从 markdown 返回的标签,并对返回值运行 geshi->parse_code(); 函数

这是我的代码

print preg_replace_callback(
   '/<pre.*?>(.*?[<pre.*?>.*<\/pre>]*)<\/pre>/gism',
    create_function(
        // single quotes are essential here,
        // or alternative escape all $ as \$
        '$matches',
        '$geshi = new GeSHi($matches[0], \'php\'); return $geshi->parse_code()'
    ),
    Markdown($blog_res['message']));

我走在正确的轨道上吗?

我的正则表达式正确吗?它适用于http://gskinner.com/RegExr/

感谢您的帮助

Im trying to get GeSHi to work with markdown.

A simple use for Geshi is as follows:

$geshi = new GeSHi($message, 'c');
print $geshi->parse_code();

The above code takes in the whole of message and turns it into Highlighted code

I also have my Markdown Function

print Markdown($message);

I was trying to use call back function to preg_match the <pre> tags returned from markdown and run the geshi->parse_code(); function on the returned values

Here is my code

print preg_replace_callback(
   '/<pre.*?>(.*?[<pre.*?>.*<\/pre>]*)<\/pre>/gism',
    create_function(
        // single quotes are essential here,
        // or alternative escape all $ as \$
        '$matches',
        '$geshi = new GeSHi($matches[0], \'php\'); return $geshi->parse_code()'
    ),
    Markdown($blog_res['message']));

Am i on the right track?

Is My Regex right? it works on http://gskinner.com/RegExr/

Thanks for the help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

a√萤火虫的光℡ 2024-10-10 17:18:36

为了便于将来参考,您可能需要查看我的插件:

https://github.com/drm/Markdown_Geshi< /a>

它基于常规 Markdown 插件,添加了一个用 shebang 标记的块来突出显示代码,如下所示:

#!php
<?php print('This is PHP code'); ?>

效果很好,我经常在自己的博客上使用它。

for future reference, you might want to check out my plugin for this:

https://github.com/drm/Markdown_Geshi

It is based on the regular markdown plugin adding a block marked with a shebang to highlight code, like this:

#!php
<?php print('This is PHP code'); ?>

Works pretty well and I use it on my own blog regularly.

ζ澈沫 2024-10-10 17:18:36

它是正则表达式 :(

而不是

/<pre.*?>(.*?[<pre.*?>.*<\/pre>]*)<\/pre>/gism 

use (删除全局标志)

/<pre.*?>(.*?[<pre.*?>.*<\/pre>]*)<\/pre>/ism 

但是如果您使用 markdown,您必须记住补偿自己的 code 块,因此您只需要替换格式为

...MyCode

并省略 Hello MyCode 因此您需要以下内容

'/<pre.*?><code.*?>(.*?[<pre.*?><code.*?>.*<\/code><\/pre>]*)<\/code><\/pre>/ism',

it was the regex :(

instead of

/<pre.*?>(.*?[<pre.*?>.*<\/pre>]*)<\/pre>/gism 

use (remove the global flag)

/<pre.*?>(.*?[<pre.*?>.*<\/pre>]*)<\/pre>/ism 

But if you are using markdown, you have to remember to compensate for the code blocks that are on thier own therefore you need to replace only the ones that are in the format of <pre><code>...MyCode</code></pre> and leave out Hello <code>MyCode</code> Therefore you need the following

'/<pre.*?><code.*?>(.*?[<pre.*?><code.*?>.*<\/code><\/pre>]*)<\/code><\/pre>/ism',
情魔剑神 2024-10-10 17:18:36

我知道您希望扩展 Markdown,添加对 GeSHi 语法突出显示的支持。 Beautify 可以做到这一点以及更多。例如,它可以用 DOT 渲染图形。

Beautify 处理 GeSHi 代码块的方法与 drm/Markdown_Geshi 不同,因为使用了“栅栏”。例如:

~~~ php
<?php print('This is PHP code'); ?>
~~~

我不确定这个问题活跃时 Beautify 是否还在,但它似乎值得在答案中提及。

I understand that you [were] looking to extend Markdown, adding support for GeSHi syntax highlighting. Beautify does this and more. For example, it can render graphs in DOT.

Beautify's approach to GeSHi code blocks differs from drm/Markdown_Geshi in that "fences" are used. For example:

~~~ php
<?php print('This is PHP code'); ?>
~~~

I'm not sure if Beautify was around back when this question was active, but it seemed worthy of mention in an answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文