Last active
June 26, 2019 14:59
-
-
Save kaityo256/a2a133daabda3163f832160554895176 to your computer and use it in GitHub Desktop.
Loading YAML in Slim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - | |
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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