Class: ReVIEW::Book::HeadlineIndex
- Defined in:
- lib/review/book/index.rb
Constant Summary collapse
- HEADLINE_PATTERN =
/\A(=+)(?:\[(.+?)\])?(?:\{(.+?)\})?(.*)/
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(chap) ⇒ HeadlineIndex
constructor
A new instance of HeadlineIndex.
- #number(id) ⇒ Object
Methods inherited from Index
#[], #add_item, #each, #item_type, #key?
Constructor Details
#initialize(chap) ⇒ HeadlineIndex
Returns a new instance of HeadlineIndex.
325 326 327 328 329 |
# File 'lib/review/book/index.rb', line 325 def initialize(chap) @chap = chap @index = {} @logger = ReVIEW.logger end |
Class Method Details
.parse(src, chap) ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/review/book/index.rb', line 258 def self.parse(src, chap) headline_index = self.new(chap) indexs = [] headlines = [] inside_column = false inside_block = nil column_level = -1 src.each do |line| if line =~ %r{\A//[a-z]+.*\{\Z} inside_block = true next elsif line.start_with?('//}') inside_block = nil next elsif inside_block next end m = HEADLINE_PATTERN.match(line) if m.nil? || m[1].size > 10 # Ignore too deep index next end index = m[1].size - 2 # column if m[2] == 'column' inside_column = true column_level = index next elsif m[2] == '/column' inside_column = false next end if indexs.blank? || index <= column_level inside_column = false end next if inside_column next if m[4].strip.empty? # no title next unless index >= 0 if indexs.size > (index + 1) unless %w[nonum notoc nodisp].include?(m[2]) indexs = indexs.take(index + 1) end headlines = headlines.take(index + 1) end if indexs[index].nil? (0..index).each do |i| indexs[i] ||= 0 end end if %w[nonum notoc nodisp].include?(m[2]) headlines[index] = m[3].present? ? m[3].strip : m[4].strip item_id = headlines.join('|') headline_index.add_item(Item.new(item_id, nil, m[4].strip)) else indexs[index] += 1 headlines[index] = m[3].present? ? m[3].strip : m[4].strip item_id = headlines.join('|') headline_index.add_item(Item.new(item_id, indexs.dup, m[4].strip)) end end headline_index end |
Instance Method Details
#number(id) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/review/book/index.rb', line 331 def number(id) unless self[id].number # when notoc return '' end n = @chap.number # XXX: remove magic number (move to lib/review/book/chapter.rb) if @chap.on_appendix? && @chap.number > 0 && @chap.number < 28 n = @chap.format_number(false) end ([n] + self[id].number).join('.') end |