2012-11-30 10 views
12

में जेनरेट किए गए मार्कअप इंडेंट करना शायद यह एक मूर्ख सवाल है लेकिन मुझे लगता है कि तरल-टैग के इंडेंटेशन को संरक्षित रखने के लिए जेकिल में जेनरेट किए गए मार्कअप का कोई तरीका है या नहीं। अगर यह हल करने योग्य नहीं है तो दुनिया खत्म नहीं होती है। मैं सिर्फ उत्सुक हूं क्योंकि मुझे अपने कोड को साफ दिखने के लिए पसंद है, भले ही संकलित हो।जेकिल/रूबी

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> 

समस्या है कि आयातित {{सामग्री}} - टैग ऊपर दिए गए इंडेन्शन के बिना प्रस्तुत किया जाता है।

तो बजाय

<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> 

लगता है केवल पहली पंक्ति की तरह सही ढंग से इंडेंट है मिलता है। बाकी लाइन की शुरुआत में शुरू होता है ... तो, मल्टीलाइन तरल-टेम्पलेटिंग आयात? :)

+0

क्या कोई भी कभी ऐसे समाधान के साथ आया है जो सीधे ठीक से इंडेंट किए गए मार्कअप का उत्पादन करता है? – mb21

उत्तर

13

एक तरल फ़िल्टर

का उपयोग करना मैं एक तरल फिल्टर का उपयोग कर यह काम करने में कामयाब रहे। कुछ चेतावनी हैं:

  • आपका इनपुट साफ़ होना चाहिए। मेरे पास कुछ घुंघराले उद्धरण और गैर-प्रिंट करने योग्य वर्ण थे जो कुछ फ़ाइलों में व्हाइटस्पेस की तरह दिखते थे (शब्द या कुछ ऐसे से कॉपीपास्टा) और जेकील त्रुटि के रूप में "यूटीएफ -8 में अमान्य बाइट अनुक्रम" देख रहे थे।

  • यह कुछ चीजें तोड़ सकता है। मैं ट्विटर बूटस्ट्रैप से <i class="icon-file"></i> आइकन का उपयोग कर रहा था। इसने <i class="icon-file"/> के साथ खाली टैग को प्रतिस्थापित किया और बूटस्ट्रैप को यह पसंद नहीं आया। इसके अतिरिक्त, यह मेरी सामग्री में octopress {% codeblock %} s को खराब करता है। मैंने वास्तव में क्यों नहीं देखा।

  • हालांकि इस तरह के {{ content }} के रूप में यह वास्तव में मूल पोस्ट, आसपास के एचटीएमएल के संदर्भ में एचटीएमएल इंडेंट करने के लिए है, जिसमें समस्या का समाधान नहीं करता है एक तरल चर के उत्पादन में साफ हो जाएगा। यह अच्छी तरह से स्वरूपित एचटीएमएल प्रदान करेगा, लेकिन एक टुकड़े के रूप में जो टुकड़े के ऊपर टैग के सापेक्ष इंडेंट नहीं किया जाएगा।यदि आप संदर्भ में सबकुछ प्रारूपित करना चाहते हैं, तो फ़िल्टर के बजाय रेक कार्य का उपयोग करें।

    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 
+0

है 'केवल' बॉडी 'तत्व की सामग्री को इंडेंट करने का एक तरीका है लेकिन पठनीयता के लिए अतिरिक्त न्यूलाइन छोड़ रहा है? – fcalderan

+0

आप यहां वर्णित एक्सएसएल ट्रांसफॉर्म को स्पष्ट रूप से निष्पादित कर सकते हैं: http://stackoverflow.com/questions/1898829/how-do-i-pretty-print-html-with-nokogiri और फिर आपको आवश्यकतानुसार एक्सएसएलटी को कस्टमाइज़ करें। – bwest

0

हम एचटीएमएल को साफ करने के लिए एक कस्टम तरल फ़िल्टर लिखकर इसे पूरा कर सकते हैं, और फिर HTML को शामिल करने के लिए {{content | tidy }} कर सकते हैं।

एक त्वरित खोज suggests कि रूबी साफ मणि को बनाए रखा नहीं जा सकता है लेकिन यह नोकोगिरी जाने का रास्ता है। यह निश्चित रूप से nokogiri मणि स्थापित करने का मतलब होगा।

advice on writing liquid filters, और Jekyll example filters देखें।

एक उदाहरण कुछ इस तरह दिख सकता है: _plugins में, एक स्क्रिप्ट बुलाया साफ-html.rb युक्त जोड़ें:

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

(untested)

+0

बस इस दृष्टिकोण की कोशिश की लेकिन पाठ अभी भी अनइंडेंट – fcalderan