在Jekyll / Ruby中缩进生成的标记

时间:2012-11-30 14:17:35

标签: html ruby jekyll liquid

这可能是一个愚蠢的问题,但我想知道是否有任何方法可以在Jekyll中生成标记以保留Liquid-tag的缩进。如果它不可解决,世界就不会结束。我很好奇,因为我喜欢我的代码看起来整洁,即使编译。 :)

例如我有这两个:

base.html文件:

<body>
    <div id="page">
        {{content}}
    </div>
</body>

index.md:

---
layout: base
---
<div id="recent_articles">
    {% for post in site.posts %}
    <div class="article_puff">
        <img src="/resources/images/fancyi.jpg" alt="" />
        <h2><a href="{{post.url}}">{{post.title}}</a></h2>
        <p>{{post.description}}</p>
        <a href="{{post.url}}" class="read_more">Read more</a>
    </div>
    {% endfor %}    
</div>

问题是导入的{{content}} - 标签是在没有上面使用的压痕的情况下渲染的。

所以而不是

<body>
    <div id="page">
        <div id="recent_articles">  
            <div class="article_puff">
                <img src="/resources/images/fancyimage.jpg" alt="" />
                <h2><a href="/articles/2012/11/14/gettin-down-with-rwd.html">Gettin' down with responsive web design</a></h2>
                <p>Everyone's talking about it. Your client wants it. You need to code it.</p>
                <a href="/articles/2012/11/14/gettin-down-with-rwd.html" class="read_more">Read more</a>
            </div>
        </div>
    </div>
</body>

我得到了

<body>
    <div id="page">
        <div id="recent_articles">  
<div class="article_puff">
<img src="/resources/images/fancyimage.jpg" alt="" />
    <h2><a href="/articles/2012/11/14/gettin-down-with-rwd.html">Gettin' down with responsive web design</a></h2>
    <p>Everyone's talking about it. Your client wants it. You need to code it.</p>
    <a href="/articles/2012/11/14/gettin-down-with-rwd.html" class="read_more">Read more</a>
</div>
</div>
    </div>
</body>

似乎只有第一行正确缩进。其余的从行首开始...那么,多行液体模板导入? :)

2 个答案:

答案 0 :(得分:13)

使用液体过滤器

我设法使用液体过滤器完成这项工作。有几点需要注意:

  • 您的输入必须干净。我有一些卷曲的引号和不可打印的字符,看起来像几个文件中的空白(来自Word或其他类似的copypasta),并且看到“UTF-8中的无效字节序列”作为Jekyll错误。

  • 它可能会破坏一些东西。我正在使用来自twitter bootstrap的<i class="icon-file"></i>图标。它用<i class="icon-file"/>替换了空标签,bootstrap不喜欢这样。另外,它会搞砸我内容中的章节{% codeblock %}。我没有真正研究过为什么。

  • 虽然这会清除液体变量的输出,例如{{ content }}但它实际上并没有解决原始帖子中的问题,即在上下文中缩进html 周围的HTML。这将提供格式良好的html,但作为一个片段,不会相对于片段上方的标签缩进。如果要在上下文中格式化所有内容,请使用Rake任务而不是过滤器。

-

require 'rubygems'
require 'json'
require 'nokogiri'
require 'nokogiri-pretty'

module Jekyll
  module PrettyPrintFilter
    def pretty_print(input)
      #seeing some ASCII-8 come in
      input = input.encode("UTF-8")

      #Parsing with nokogiri first cleans up some things the XSLT can't handle
      content = Nokogiri::HTML::DocumentFragment.parse input
      parsed_content = content.to_html

      #Unfortunately nokogiri-pretty can't use DocumentFragments...
      html = Nokogiri::HTML parsed_content
      pretty = html.human

      #...so now we need to remove the stuff it added to make valid HTML
      output = PrettyPrintFilter.strip_extra_html(pretty)
      output
    end

    def PrettyPrintFilter.strip_extra_html(html)
      #type declaration
      html = html.sub('<?xml version="1.0" encoding="ISO-8859-1"?>','')

      #second <html> tag
      first = true
      html = html.gsub('<html>') do |match|
        if first == true
          first = false
          next
        else
          ''
        end
      end

      #first </html> tag
      html = html.sub('</html>','')

      #second <head> tag
      first = true
      html = html.gsub('<head>') do |match|
        if first == true
          first = false
          next
        else
          ''
        end
      end

      #first </head> tag
      html = html.sub('</head>','')

      #second <body> tag
      first = true
      html = html.gsub('<body>') do |match|
        if first == true
          first = false
          next
        else
          ''
        end
      end

      #first </body> tag
      html = html.sub('</body>','')

      html
    end
  end
end

Liquid::Template.register_filter(Jekyll::PrettyPrintFilter)

使用佣金任务

我在rakefile中使用了一个任务,在生成jekyll网站后打印输出。

require 'nokogiri'
require 'nokogiri-pretty'

desc "Pretty print HTML output from Jekyll"
task :pretty_print do
  #change public to _site or wherever your output goes
  html_files = File.join("**", "public", "**", "*.html")

  Dir.glob html_files do |html_file|
    puts "Cleaning #{html_file}"

    file = File.open(html_file)
    contents = file.read

    begin
      #we're gonna parse it as XML so we can apply an XSLT
      html = Nokogiri::XML(contents)

      #the human() method is from nokogiri-pretty. Just an XSL transform on the XML.
      pretty_html = html.human
    rescue Exception => msg
      puts "Failed to pretty print #{html_file}: #{msg}"
    end

    #Yep, we're overwriting the file. Potentially destructive.
    file = File.new(html_file,"w")
    file.write(pretty_html)

    file.close
  end
end

答案 1 :(得分:0)

我们可以通过编写自定义Liquid过滤器来整理html,然后执行{{content | tidy }}来包含html来实现这一目标。

快速搜索suggests红宝石整齐的宝石可能无法维持,但nokogiri是要走的路。这当然意味着安装nokogiri宝石。

请参阅advice on writing liquid filtersJekyll example filters

示例可能如下所示:在_plugins中,添加一个名为tidy-html.rb的脚本,其中包含:

require 'nokogiri'
module TextFilter
  def tidy(input)
  desired = Nokogiri::HTML::DocumentFragment.parse(input).to_html
  end
end
Liquid::Template.register_filter(TextFilter)

(未测试的)