如何在markdown文件中传递和呈现ruby变量?宝石“红地毯" [英] How to pass and render a ruby variable in a markdown file? Gem "redcarpet"

查看:70
本文介绍了如何在markdown文件中传递和呈现ruby变量?宝石“红地毯"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用宝石"redcarpet".而且我有一个markdown文件.我希望能够使用一些红宝石变量进行渲染.像这样:

I'm using the gem "redcarpet". And I have a markdown file. I want to be able to render it with some ruby variables. Something like this:

 # my_file.md

 ###Something
 fdafdsfdsfds

 ---

 <% for n in my_numbers do %>
     <%= n %>
 <% end %>

做这样的事情的正确方法是什么?如何传递和渲染红宝石 变量?

What's the proper way to do something like this? How can I pass and render a ruby variable?

推荐答案

您可以使用ruby中包含的erb库,但是您需要使用它:

You can use the erb library which is included in ruby, but you have to require it:

require 'erb'
require 'redcarpet'

input = File.read "./file.md"

markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)

output = markdown.render ERB.new(input).result(binding)

File.open("output.html", "w") { |f| f.write output }

您可以通过引用 redcarpet自述文件

这篇关于如何在markdown文件中传递和呈现ruby变量?宝石“红地毯"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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