Skip to content

Instantly share code, notes, and snippets.

@kaityo256
Last active June 26, 2019 14:59
Show Gist options
  • Save kaityo256/a2a133daabda3163f832160554895176 to your computer and use it in GitHub Desktop.
Save kaityo256/a2a133daabda3163f832160554895176 to your computer and use it in GitHub Desktop.
Loading YAML in Slim
require "yaml"
class Paper
def initialize(h)
@h = h
@h.each_key do |key|
self.class.send(:define_method, key.to_sym) do
@h[key]
end
self.class.send(:define_method, ("has_"+key+"?").to_sym) do
@h.has_key? key
end
end
def arxiv_ref
"<a href=\"https://arxiv.org/abs/#{arxiv}\">arXiv:#{arxiv}</a>"
end
def journal_ref
jtext = "#{journal}, <strong>#{volume}</strong>, #{page} (#{year})"
jtext
end
end
end
def load_file(filename)
papers = []
YAML.load_file(filename).each do |d|
papers.push Paper.new(d)
end
papers
end
load_file("publication.yaml").each do |paper|
puts paper.title
puts paper.author
puts paper.journal_ref
puts paper.arxiv_ref if paper.has_arxiv?
end
-
title: How to write web sites with slim
author: R. Robota, T. Tanaka
journal: Ruby Journal
year: 2010
volume: 10
page: 1-7
arxiv: 1234.5678
-
title: Using YAML file with slim
author: R. Robot
journal: Journal of Slim
year: 2018
volume: 8
page: 1275
<!DOCTYPE html>
<html>
<head>
<title>Publication List</title>
</head>
<body>
<h1>
Publication List
</h1>
<ol>
<li>
<dl>
<dt>
How to write web sites with slim
</dt>
<dd>
Ruby Journal, <strong>10</strong>, 1-7 (2010)
</dd>
<dd>
R. Robota, T. Tanaka
</dd>
<dd>
arXiv:1234.5678
</dd>
</dl>
</li>
<li>
<dl>
<dt>
Using YAML file with slim
</dt>
<dd>
Journal of Slim, <strong>8</strong>, 1275 (2018)
</dd>
<dd>
R. Robot
</dd>
</dl>
</li>
</ol>
</body>
</html>
doctype html
html
- require 'yaml'
head
title Publication List
body
h1 Publication List
ol
- YAML.load_file("publication.yaml").each do |d|
li :dl
dt = d["title"]
dd == "#{d["journal"]}, <strong>#{d["volume"]}</strong>, #{d["page"]} (#{d["year"]})"
dd = d["author"]
- if d.has_key? "arxiv"
dd = d["arxiv"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment