Class: DocxConverter::PostProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/docx_converter/postprocessor.rb

Instance Method Summary collapse

Constructor Details

#initialize(content, footnote_definitions) ⇒ PostProcessor

Returns a new instance of PostProcessor.



23
24
25
26
27
28
# File 'lib/docx_converter/postprocessor.rb', line 23

def initialize(content, footnote_definitions)
  @content = content
  @footnote_definitions = footnote_definitions
  
  @chapters = []
end

Instance Method Details

#add_foonote_definitionsObject



64
65
66
67
68
69
70
71
72
# File 'lib/docx_converter/postprocessor.rb', line 64

def add_foonote_definitions
  @chapters.size.times do |n|
    footnote_ids = @chapters[n].scan(/\[\^(.+?)\]/).flatten
    @chapters[n] += "\n\n"
    footnote_ids.each do |i|
      @chapters[n] += "[^#{ i }]: #{ @footnote_definitions[i] }\n\n"
    end
  end
end

#chaptersObject

getter method



31
32
33
# File 'lib/docx_converter/postprocessor.rb', line 31

def chapters
  return @chapters
end

#join_blockquotesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/docx_converter/postprocessor.rb', line 35

def join_blockquotes
  lines = @content.split("\n")
  processed_lines = []
  lines.size.times do |i|
    if /^> /.match(lines[i-1]) && /^> /.match(lines[i+1])
      processed_lines << ">" + lines[i]
    else
      processed_lines << lines[i]
    end
  end
  @content = processed_lines.join("\n")
  @chapters[0] = @content
  return @content
end

#split_into_chaptersObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/docx_converter/postprocessor.rb', line 50

def split_into_chapters
  chapter_number = 0
  @chapters[chapter_number] = ""
  @content.split("\n").each do |line|
    if /^# /.match(line)
      # this is the style Heading1. A new chapter begins here.
      chapter_number += 1
      @chapters[chapter_number] = ""
    end
    @chapters[chapter_number] += line + "\n"
  end
  return @chapters
end