rus*_*gen 14 html ruby liquid jekyll
嗯,这可能是一个愚蠢的问题,但我想知道是否有任何方法可以在Jekyll中生成标记以保留Liquid-tag的缩进.如果它不可解决,世界就不会结束.我很好奇,因为我喜欢我的代码看起来整洁,即使编译.:)
例如,我有这两个:
base.html文件:
<body>
<div id="page">
{{content}}
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
问题是导入的{{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>
Run Code Online (Sandbox Code Playgroud)
我明白了
<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>
Run Code Online (Sandbox Code Playgroud)
似乎只有第一行正确缩进.其余的从行首开始...那么,多行液体模板导入?:)
bwe*_*est 13
使用液体过滤器
我设法使用液体过滤器完成这项工作.有几点需要注意:
你的输入必须干净.我有一些卷曲的引号和不可打印的字符看起来像几个文件中的空白(来自Word或其他类似的copypasta),并且看到"UTF-8中的无效字节序列"作为Jekyll错误.
它可能会破坏一些东西.我正在使用<i class="icon-file"></i>来自twitter bootstrap的图标.它取代了空标签,<i class="icon-file"/>并且bootstrap不喜欢这样.另外,它{% codeblock %}在我的内容中搞砸了octopress .我没有真正研究为什么.
虽然这将清除液体变量的输出,例如{{ 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)
Run Code Online (Sandbox Code Playgroud)
使用Rake任务
我在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
Run Code Online (Sandbox Code Playgroud)