Rails haml 代码块输出问题

shatle · April 21, 2013 · Last by shatle replied at April 21, 2013 · 3015 hits

有一由 markdown 转出来的字符串,

<pre><code class=\"lang-ruby\">class Code\r\n  def hello\r\n  end\r\nend</code></pre>

注意中间\r\n后都只是两个空格的,在 view 中有页面如

%div
  = sanitize from_md_string

期望浏览器上的结果是

class Code
  def hello
  end
end

但真正显示的是

class Code
                def hello
                end
              end

后面三行,差了 14 个位,怎么回事?

我想让实际显示如期望显示的一样。

在命令行中,测试了 sanitize 方法输出的字符串,仍然是

<pre><code class=\"lang-ruby\">class Code\r\n  def hello\r\n  end\r\nend</code></pre>

也就是 sanitize 输出没有错啊,应该!

又考虑到可能是 css 的原因,但是用了 bootstrap 还是这样子。

那是不是 haml 的问题呢?查了一下有:

http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace_preservation

但是不知从何下手,麻烦大家帮忙一下!

这个你使用 haml 的 preserve filter 就可以处理了。

You need to Sign in before reply, if you don't have an account, please Sign up first.