Class: MyLastCV::Parser
- Inherits:
-
Object
- Object
- MyLastCV::Parser
- Defined in:
- lib/my_last_cv/parser.rb
Instance Method Summary collapse
-
#initialize(markdown) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
Constructor Details
#initialize(markdown) ⇒ Parser
Returns a new instance of Parser.
3 4 5 |
# File 'lib/my_last_cv/parser.rb', line 3 def initialize(markdown) @markdown = markdown end |
Instance Method Details
#parse ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/my_last_cv/parser.rb', line 7 def parse lignes = @markdown.lines.map(&:chomp) resultat = { sections: [] } section_actuelle = nil lignes.each do |ligne| next if ligne.strip.empty? if (m = ligne.match(/^#\s+(.*)/)) resultat[:name] = m[1].strip elsif (m = ligne.match(/^email:\s*(.+)/i)) resultat[:contact] ||= [] resultat[:contact] << m[1].strip elsif (m = ligne.match(/^location:\s*(.+)/i)) resultat[:contact] ||= [] resultat[:contact] << m[1].strip elsif (m = ligne.match(/^##\s+(.*)/)) section_actuelle = { title: m[1].strip, items: [] } resultat[:sections] << section_actuelle elsif (m = ligne.match(/^[-*]\s+(.*)/)) section_actuelle ||= { title: "Divers", items: [] } resultat[:sections] << section_actuelle unless resultat[:sections].include?(section_actuelle) section_actuelle[:items] << m[1].strip end end resultat[:contact] = (resultat[:contact] || []).join(" · ") resultat end |