Class: MD
Defined Under Namespace
Classes: Page
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
- #__filename_sanitize(s) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(path) ⇒ MD
constructor
A new instance of MD.
- #load(path) ⇒ Object
- #parse_header(text) ⇒ Object
- #parse_page(text) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(path) ⇒ MD
Returns a new instance of MD.
76 77 78 79 80 |
# File 'lib/md2slides/md.rb', line 76 def initialize(path) @pages = [] @attributes = {} load(path) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
74 75 76 |
# File 'lib/md2slides/md.rb', line 74 def attributes @attributes end |
Instance Method Details
#__filename_sanitize(s) ⇒ Object
82 83 84 |
# File 'lib/md2slides/md.rb', line 82 def __filename_sanitize(s) s.gsub(/[\/\\:\*\?"<>\|]/, '') end |
#each(&block) ⇒ Object
86 87 88 |
# File 'lib/md2slides/md.rb', line 86 def each(&block) @pages.each(&block) end |
#load(path) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/md2slides/md.rb', line 151 def load(path) File.read(path).split('---').each do |text| if @attributes.empty? parse_header(text) next end parse_page(text) end end |
#parse_header(text) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/md2slides/md.rb', line 94 def parse_header(text) text.each_line do |l| l.strip! next if l.empty? if l =~ /^([^:]+): *([^ ].*)$/ k, v = $1.strip, $2.strip @attributes[k.to_sym] = v else raise("ERROR: invalid line in a header: #{l}") end end end |
#parse_page(text) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/md2slides/md.rb', line 107 def parse_page(text) @pages << page = Page.new is_in_comment = false text.each_line do |l0| l = l0.strip next if l.empty? if is_in_comment if l =~ /(.*) ?-->(.*)$/ c, left = $1, $2 page.add_comment(c) page.add(:p, left) is_in_comment = false else page.add_comment(l) end else case l when /^(#+) *(.*)$/ sharps, title = $1, $2 h = "h#{sharps.size}" page.add(h.to_sym, title) when /^[-*] *(.*)$/ s = $1 if l0 =~ /^( +).*$/ indent = { indent: $1.size } end page.add(:li, s, indent) when /^<!-- *(.*)$/ l = $1 if l =~ /(.*) ?-->(.*)$/ c, left = $1, $2 page.add_comment(c) page.add(:p, left) else is_in_comment = true page.add_comment(l) end else page.add(:p, l) end end end end |
#size ⇒ Object
90 91 92 |
# File 'lib/md2slides/md.rb', line 90 def size @pages.size end |