倾斜(kramdown)可防止在渲染降价时进行ERB处理 [英] Tilt (kramdown) preventing ERB processing when rendering markdown

查看:83
本文介绍了倾斜(kramdown)可防止在渲染降价时进行ERB处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Middleman 建立一个网站.我将大量信息存储在数据文件中,因为我将在多个页面上使用相同的信息. (部分内容对我不起作用,因为来自数据的相同文本可以与不同的HTML标签一起使用,或者可以对不同的页面进行略微修改.)

I am building a website with Middleman. I am storing a lot of information in data files, as I am going to use the same information on multiple pages. (Partials wouldn't work for me, as same text from data can be used with different HTML tags or be slightly modified for different pages.)

我想在数据文件中写入 markdown ,然后在HAML模板内将其用于特定页面.当我尝试在使用对其他数据文件的引用的同时创建到其他页面的相对链接时,结果HTML并不是应该的.

I want to write markdown in data files and then use it within HAML templates for specific pages. When I try to create relative links to other pages while using a reference to other data file, resulting HTML is not what it should be.

data/pages.yaml:

pageA:
  link: /it-can-change-A.html
  name: PageA name
  info: Some other related info

pageB:
  link: /subject-to-change-B.html
  name: PageB name
  info: Some other related info

pageC:
  link: /some-C.html
  name: PageC name
  info: Some other related info

data/faq.yaml:

testcase: Some text with [internal link to page A](ref.pageA). And *another* [internal reference](ref.pageB).

verbatim: Some text with [internal link to page A](/it-can-change-A.html). And *another* [internal reference](/subject-to-change-B.html).

在尝试查找此问题的根本原因时,我测试了多种方法来从数据文件中提取"相对链接并将其放入HAML模板中.我的最终目标是使案例(5)发挥作用.

While trying to find the root cause of this issue, I have tested various ways to "extract" relative links from the data file and put them in the HAML template. My end goal is to get the case (5) to work.

我直接在test.html.md.erb.haml中评论了每段代码的结果:

I commented on the results of each piece of code directly in my test.html.md.erb.haml:

请注意,必须在ERB之前处理HAML,因此.haml必须是最后一个扩展名.如果.erb和.haml扩展名切换位置,则下面的情况(3)和(4)将产生不同的输出.

/ Pure ERB; ERB is processed before markdown => :)
(1) This is just some text with [ERB link to first page](<%= data.pages.pageA.link %>) and *another* one [to second page](<%= data.pages.pageB.link %>). 

/ ERB inside HAML tag; markdown is not processed => :|
.haml
    (2) This is just some text with [ERB link to first page](<%= data.pages.pageA.link %>) and *another* one [to second page](<%= data.pages.pageB.link %>). 

/ Helper used WITHOUT a tag; ERB is processed before markdown => :)
(3)
= refonly(data.faq.testcase) 

/ Helper used WITH a tag; ERB is processed, but markdown is not => :|
.question1
    (4)
    = refonly(data.faq.testcase)

/ "Tilt-powered" helper used WITHIN a tag; trying to process markdown with Tilt results in "%=%20data.pages.pageA.link%20%" links. Expected behavior: ERB should be processed first, then Tilt should process markdown with actual relative links being the same as in .question1 above => :(
.question2
    (5)
    = mymarkdown(data.faq.testcase)

/ Helper with Tilt to process verbatim text; markdown is processed correctly => :)
.question3
    (6)
    = justmarkdown(data.faq.verbatim)

上面模板中使用的助手:

Helpers used in template above:

def refonly(text)
    text.gsub(/ref\.(page[A-Z])/,"<\%= data.pages.\\1.link %>")
end

def mymarkdown(text)
    newtext = refonly(text)
    Tilt['markdown'].new(context: @app) { newtext }.render
end

def justmarkdown(text)
    Tilt['markdown'].new(context: @app) { text }.render
end

第一个帮助程序的目的是将数据文件(ref.pageName)中易于编写的引用更改为ERB代码,与(1)中使用的相同.第二个帮助程序的目的是在.md模板扩展本身无法自动呈现它的情况下呈现markdown.最后一个助手的目的是为了证明Tilt可以在(6)中正确呈现逐字记录文本,但是当它接受相同的文本作为变量时,它不能在(5)中提供相同的输出.

The purpose of the first helper is to change easy-to-write reference inside data file (ref.pageName) to ERB code, same as one used in (1). The purpose of the second helper is to render markdown in the case where .md template extension itself fails to render it automatically. The purpose of the last helper is to show that Tilt can render verbatim text properly in (6), but it when it accepts same text as a variable, it can't provide same output in (5).

A.当我使用纯ERB"(1)或没有HAML标签的refonly帮助器(3)时,输出是预期的:相对路径来自数据文件,然后默认使用markdown引擎(kramdown)处理markdown,这要归功于.md模板扩展名.

A. When I use "pure ERB" (1), or refonly helper without HAML tag (3), output is as expected: relative path is sourced from data file and then markdown is processed by default markdown engine (kramdown) thanks to .md template extension.

B.当我尝试直接使用ERB代码(2)或使用refonly辅助程序(4)使用HAML标签时,将正确处理ERB并将其放入输出中.但是,即使在.html之后直接使用.md扩展名,由于某些原因也不自动处理markdown,因此应在HAML和ERB位完成后处理markdown.

B. When I try to use HAML tag, either directly with ERB code (2), or with refonly helper (4), ERB is processed correctly and put into the output. However, markdown is not processed for some reason automatically, even though .md extension is used directly after .html, hence markdown should be processed after HAML and ERB bits are finished.

C.为了强制"降价渲染,我在第二个助手中使用了Tilt.我的意图是将refonly返回的文本(具有正确的markdown语法以及从数据中提取的预期链接)传递给Tilt.我的期望是,Tilt会像在(6)中那样简单地渲染在(5)中传递给它的逐字记录文本.而是,结果链接指向%=%20data.pages.pageA.link%20%,它似乎是HTML代码,用于逐字显示ERB代码.因此,将newtext变量传递给Tilt似乎以某种方式停止了ERB处理,并将ERB代码直接传递给markdown.

C. In order to "force" markdown rendering, I am using Tilt in my second helper. My intention was to pass text returned by refonly (which has correct markdown syntax with intended link extracted from data) to Tilt. My expectation was that Tilt would simply render verbatim text passed to it in (5), just as it does in (6). Instead, resulting link points to %=%20data.pages.pageA.link%20%, which seems to be HTML code to display ERB code verbatim. So, it looks like passing newtext variable to Tilt somehow stops ERB processing and ERB code is passed directly to markdown.

我的主要问题是:如何确保Tilt获得带有相对链接的正确文本(由refonly返回)并在(5)中产生预期的输出?

My primary question is: how can I make sure that Tilt gets the proper text with a relative link (as returned by refonly) and produces expected output in (5)?

我的次要问题:为什么在B中所述的情况下,中间人不自动处理降价交易??

My secondary question: why markdown is not processed automatically by Middleman in cases described under B.?

我认为,回答我的主要问题需要了解Ruby和Tilt,而回答我的第二个问题则需要了解Middleman.尽管我的首要问题的解决方案很好,但回答第二个问题可能会完全跳过Tilt,从而以更简单的方式解决问题.

I assume that answer to my primary question requires knowledge of the Ruby and Tilt, while the answer to my secondary question requires knowledge of Middleman. While the solution to my primary issue would be great, answer to the secondary question might allow to skip Tilt altogether and hence solve the problem in an easier way.

推荐答案

由于您已经正确解析了HAML,并且在呈现逐字链接时可以正常使用,请尝试使用帮助程序直接注入链接,而无需转到ERB.由于HAML已经可以运行Ruby代码,因此我也无需将其通过ERB传递.我认为eval将使您获得与使用ERB相同的效果.像这样:

Since you already have HAML parsing correctly, and when you render verbatim links it works, try using a helper to directly inject the links without needing to go to ERB. Since HAML will already run Ruby code, I see no need to pass it through ERB too. I think eval will allow you to get the same effect you're using ERB for. Something like this:

def refonly(text)
  text.scan(/ref\.(page[A-Z])/).each do |groups|
    page_name = groups[0]
    text.gsub!(/ref\.#{page_name}/, eval("data.pages.#{page_name}.link"))
  end
  text
end

扫描文本将使您能够获取每个page_name,因此您可以循环浏览每个page_name并将每个页面引用替换为其链接.

Scanning the text will allow you to get each page_name so you can loop through each one and replace each page reference with its link.

这篇关于倾斜(kramdown)可防止在渲染降价时进行ERB处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆