Middleman:从Markdown引用存储在数据文件中的URL [英] Middleman: referencing URL stored in a data file from markdown

查看:121
本文介绍了Middleman:从Markdown引用存储在数据文件中的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的由中间人创建的网站,我已将链接和其他有关所有页面的信息存储在数据文件中.

data/pages.yaml:

pageA:
  link: /some-long-url-subject-to-change.html
  name: PageA name
  info: Some other related info

然后,在我的HAML模板(source/test.haml)中,我可以使用= data.pages.pageA.link打印到pageA的相对路径.

Then, in my HAML template (source/test.haml), I can print relative path to pageA with = data.pages.pageA.link.

现在,我想使用markdown语法按页面名称(pageA)引用该页面.

Now, I want to use markdown syntax to reference that page by its name (pageA).

示例(source/test.html.haml):

.info
    :markdown
        This is some text with a [manual link](https://google.com) to somewhere. 
        This is another text with a [data-referenced link](pageA) to that page.

与第一个手动链接"到Google的链接一样,我希望第二个链接使用存储在数据文件中的相对路径创建一个链接.我认为可以解决此问题的一种解决方案是,先将(pageA)文本替换为评估为= data.pages.pageA.link的文本,然后再通过markdown呈现它.

In the same way as first "manual link" links to Google, I would like second link to use relative path stored in data file to create a link. One solution that I see to solve this problem would be to replace (pageA) text with evaluation of = data.pages.pageA.link prior to it being rendered by markdown.

我认为可以通过创建自定义帮助程序来实现,但是我不太确定.

I assume this would be possible by creating custom helper, but I can't quite nail it.

我试图编写一个自定义帮助程序,以便在Markdown呈现= data.pages.pageA.link文本之前用= data.pages.pageA.link评估替换(pageA)文本.

I tried to write a custom helper to replace (pageA) text with evaluation of = data.pages.pageA.link prior to it being rendered by markdown.

我能够用来自数据的信息替换特定的文本(pageA),而且我还能够编写更通用的用例,用典型数据引用的显式文本替换所有数据引用.但是我无法在一般情况下替换data.pages.pageA.link来评估= data.pages.pageA.link.

I was able to replace specific text (pageA) with information from data and I was also able to write more generic case, which replaces all data references with explicit text of typical data reference. But I can't get to replace data.pages.pageA.link in generic case for evaluation of = data.pages.pageA.link.

我的助手:

# Replace specific text with information from ``data/pages.yaml``
specific = text.gsub("pageA",data.pages.pageA.link)
# Generic case: using explicit text
generic = text.gsub(/\]\((.*?)\)/,'](data.pages.\1.link)')
# Generic case: trying to use variable name, but getting explicit text
generic = text.gsub(/\]\((.*?)\)/,'](#{data.pages.\1.link})')

test.html.haml中的助手的用法:

= myhelper("This is another text with a [data-referenced link](pageA) to that page.")

打印specific变量会给我我想要的内容(/some-long-url-subject-to-change.html).但是打印generic变量会产生纯文本,而不是数据文件中的信息.

Printing specific variable gives me what I want (/some-long-url-subject-to-change.html). But printing generic variable results in plain text, instead of information from data file.

我可能缺少一些基本的Ruby知识,并且解决方案的确非常简单.

It is possible that I am lacking some basic Ruby knowledge and solution is indeed very simple.

推荐答案

似乎您正在尝试编写自己的模板系统,这可能不是最好的主意.首先,您需要编写一个可正确解析Markdown并找到链接符号的正则表达式.然后,您需要评估提取的字符串以从数据中获取值.最后,您需要用该值替换占位符.我或多或少地在config.rb中使用了此代码:

It looks as if you are trying to write your own templating system, which is probably not the best idea. First, you'll need to write a regex that properly parses Markdown and finds link notation. Then you need to eval the string you pulled to get the value from your data. And finally you'll need to substitute the placeholder with that value. I more or less got that working with this code in config.rb:

helpers do
  def myhelper(text)
    page=/\]\((.*?)\)/.match(text)[1]
    link=eval( "data.pages.#{page}.link" )
    text.gsub(page,link)
  end
end

但是请不要使用此代码!这是脆弱且容易出错的代码.更糟糕的是,有一种更简单的方法可以执行您要执行的操作.将您的test.haml替换为test.haml.erb:

But please don't use this code! It's fragile and error-prone. Worse, there's a much simpler way to do what you are trying to do. Replace your test.haml with test.haml.erb:

.info
    :markdown
        This is some text with a [manual link](https://google.com) to somewhere. 
        This is another text with a [data-referenced link](<%= data.pages.pageA.link %>) to that page.

= myhelper("This is another text with a [data-referenced link](pageA) to that page.")

.erb扩展名告诉Middleman将文件视为 ERB模板.特别是,它将评估<%=%>之间的任何内容作为Ruby代码.这意味着您可以跳过使用杂乱的辅助方法.另外,您无需编写更多代码即可获取其他数据:

The .erb extension tells Middleman to treat the file as an ERB template. In particular, it'll evaluate whatever is between <%= and %> as Ruby code. That means you can skip using a messy helper method. As a bonus, you don't need to write more code to get the other bits of data:

  • <%= data.pages.pageA.name %> =>'PageA名称'
  • <%= data.pages.pageA.info %> =>'其他一些相关信息'
  • <%= data.pages.pageA.name %> => 'PageA name'
  • <%= data.pages.pageA.info %> => 'Some other related info'

如果需要进行一些更复杂的处理,例如遍历列表,则可以在模板本身中进行处理.这样更容易阅读和维护.

If you need to do some more complex processing, such as iterating over a list, you can do that right in the template itself. It's easier to read and maintain that way.

作为对比,我还包括了对helper方法的调用.与仅使用现有的模板系统相比,尚不清楚发生了什么.另外,您一定会忘记将所有相关的字符串包装在方法中,并最终导致链接断开.

I also included a call to the helper method as contrast. It's a lot less clear what's going on than just using the existing templating system. Plus, you are bound to forget to wrap all the relevant strings in the method and end up with broken links down the road.

按照 Adam的建议,直接使用Markdown并跳过

As Adam suggested, you might be even better off using Markdown directly and skipping Haml.

这篇关于Middleman:从Markdown引用存储在数据文件中的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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