如何扩展redcarpet以支持自动链接用户提及? [英] How to extend redcarpet to support auto linking user mentions?

查看:82
本文介绍了如何扩展redcarpet以支持自动链接用户提及?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的rails3应用程序上,我想使用redcarpet处理用户的帖子和用户评论部分.因此,我想扩展redcarpet以支持将@username转换为指向我网站上用户的链接.我知道redcarpet是用C编写的,但是有没有简单的方法可以用ruby扩展它呢?用C语言编写它会有多困难?我应该在红地毯外面做这个吗?

On my rails3 application I want to use redcarpet to handle user's posts and the user comment section. As such I'd like to extend redcarpet to support turning @username into a link to a user on my site. I know redcarpet is written in C but is there anyway easy way to extend it in ruby? How hard would it be to write it in C? Should I just do this outside of redcarpet?

此外,我对redcarpet的其他一些扩展很着迷,这些扩展是链接到我的应用程序中其他模型的简写.我尚不确定语法,但我猜测它与github处理链接到问题的方式类似.

Also I'm intrested in some other extensions of redcarpet that would be shorthand for linking to other models in my app. I'm not sure the syntax yet but I'm guessing it would be similar to how github handles linking to issues.

推荐答案

我发现在Rails 3应用程序中使用Ruby扩展redcarpet的解析器非常容易.一点也不可怕.

I found it pretty easy to extend redcarpet's parser in Ruby for my rails 3 app. It wasn't scary at all.

首先,首先从Redcarpet的HTML渲染器派生一个类,然后按照文档中的建议覆盖preprocess方法.在Rails 3.2和Rails 4中,此文件可以放在任何地方,您不需要它.我使用服务"文件夹来保存这样的代码.

First, start by deriving a class from Redcarpet's HTML renderer and override the preprocess method as recommended in the docs. In Rails 3.2 and Rails 4, this file can go anywhere and you don't need to require it. I use a 'services' folder to hold code like this.

# app/services/my_flavored_markdown.rb
class MyFlavoredMarkdown < Redcarpet::Render::HTML
  def preprocess(text)
    text
  end
end

下一步是添加执行所需文本替换的方法.在这里,我使用正则表达式将带有css类"mention"的HTML跨度标签中看起来像"@mention"的文本换行.

Next step is to add methods that do text substitutions you want. Here I use regex to wrap text that looks like "@mention" in an HTML span tag with a css class 'mention'.

# app/services/my_flavored_markdown.rb
class MyFlavoredMarkdown < Redcarpet::Render::HTML

  def preprocess(text)
    wrap_mentions(text)
  end

  def wrap_mentions(text)
    text.gsub! /(^|\s)(@\w+)/ do
      "#{$1}<span class='mention'>#{$2}</span>"
    end
    text
  end

end

您可以轻松地查找用户的个人资料页面,然后将@mention包裹在锚标记中.就我而言,我还制作了表情符号和主题标签的方法,它们的工作方式相同,并将这些方法链接在一起.

You could just as easily look up a user's profile page and wrap the @mention in an anchor tag instead. In my case, I also made methods for emoticons and hashtags that worked the same way and chained the methods together.

最后一步是添加一个接受一些文本的帮助程序,创建Redcarpet派生类的实例,将文本传递给该类进行处理,然后返回html结果.

The last step is to add a helper that accepts some text, creates an instance of your Redcarpet-derived class, passes the text into that for processing, and returns the html result.

# app/helpers/application_helper.rb
def flavored_markdown_to_html(text)
  renderer = MyFlavoredMarkdown.new()
  # These options might be helpful but are not required
  options = {
    safe_links_only: true,
    no_intra_emphasis: true,
    autolink: true
  }
  Redcarpet::Markdown.new(renderer, options).render(text)
}

在您看来,您可以这样称呼它:

In your views you can call it like this:

<%= flavored_markdown_to_html("This is something worth @mentioning") %>

输出将是:

This is something worth <span class='mention'>@mentioning</span>.

这篇关于如何扩展redcarpet以支持自动链接用户提及?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆