2

我正在为基于 Jekyll 的网站编写 Redcarpet 的扩展。我想{x|y}在降价中用作一个标签,它评估为 HTML<ruby>标签(及其关联)。我按照Jekyll 的指南Redcarpet 的指南以及如何做到这一点的指南编写了这门课:

class Jekyll::Converters::Markdown::HotelDown < Redcarpet::Render::HTML
    def preprocess(doc)
        s = "<ruby><rb>\\1</rb><rp>(</rp><rt>\\2</rt><rp>)</rp></ruby>"
        doc.gs­ub!(/\[([\­s\S]+)\|([­\s\S]+)\]/­, s)
        doc
    end
end

但是,当我运行时,我似乎遇到了几个错误bundle exec jekyll serve

Configuration file: C:/Users/Alex/OneDrive/codes/hotelc.me/hotelc.me/_config.yml
plugin_manager.rb:58:in `require': HotelDown.rb:4: syntax error, unexpected tIDENTIFIER, expecting ')' (SyntaxError)
            doc.gs-ub!(/\[([\-s\S]+)\|([-\s\S]+)\]/-, s)
                                                        ^
HotelDown.rb:4: syntax error, unexpected ')', expecting '='
            doc.gs-ub!(/\[([\-s\S]+)\|([-\s\S]+)\]/-, s)
                                                            ^

我的语法似乎有问题(额外的空格、缺少括号或类似的东西)。有什么我错过的吗?

4

1 回答 1

3

您的代码有一些导致此错误的特殊字符:

syntax error, unexpected ')', expecting '='
            doc.gs-ub!(/\[([\-s\S]+)\|([-\s\S]+)\]/-, s)

用这段代码替换您当前的代码:

class Jekyll::Converters::Markdown::HotelDown < Redcarpet::Render::HTML
  #Overriding the preprocess() function
  def preprocess(doc)
    s = "<ruby><rb>\\1</rb><rp>(</rp><rt>\\2</rt><rp>)</rp></ruby>"
    doc.gsub!(/\[([\s\S]+)\|([\s\S]+)\]/, s)
    doc
  end
end

markdown = Redcarpet::Markdown.new(HotelDown)

它应该工作!

于 2015-10-14T03:56:00.603 回答