Class: Slaw::ActGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/slaw/generator.rb

Overview

Base class for generating Act documents

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActGenerator

Returns a new instance of ActGenerator.



15
16
17
18
19
20
# File 'lib/slaw/generator.rb', line 15

def initialize
  @parser = Slaw::ZA::ActParser.new
  @builder = Slaw::Parse::Builder.new(parser: @parser)
  @cleanser = Slaw::Parse::Cleanser.new
  @document_class = Slaw::Act
end

Instance Attribute Details

#builderObject

Slaw::Parse::Builder

builder used by the generator



10
11
12
# File 'lib/slaw/generator.rb', line 10

def builder
  @builder
end

#document_classObject

The type that will hold the generated document



13
14
15
# File 'lib/slaw/generator.rb', line 13

def document_class
  @document_class
end

#parserObject

Treetop::Runtime::CompiledParser

compiled parser



7
8
9
# File 'lib/slaw/generator.rb', line 7

def parser
  @parser
end

Instance Method Details

#cleanup(text) ⇒ Object

Run basic cleanup on text, such as ensuring clean newlines and removing tabs. This is always automatically done before processing.



36
37
38
# File 'lib/slaw/generator.rb', line 36

def cleanup(text)
  @cleanser.cleanup(text)
end

#generate_from_text(text) ⇒ Slaw::Act

Generate a Slaw::Act instance from plain text.

Parameters:

  • text (String)

    plain text

Returns:



27
28
29
30
31
# File 'lib/slaw/generator.rb', line 27

def generate_from_text(text)
  act = @document_class.new
  act.doc = @builder.parse_and_process_text(cleanup(text))
  act
end

#guess_section_number_after_title(text) ⇒ Object

Try to determine if section numbers come after titles, rather than before.

eg:

Section title
1. Section content

versus

1. Section title
Section content


59
60
61
62
63
64
# File 'lib/slaw/generator.rb', line 59

def guess_section_number_after_title(text)
  before = text.scan(/^\w{4,}[^\n]+\n\d+\. /).length
  after  = text.scan(/^\s*\n\d+\. \w{4,}/).length

  before > after * 1.25
end

#reformat(text) ⇒ Object

Reformat some common errors in text to help make parsing more successful. Option and only recommended when processing a document for the first time.



43
44
45
# File 'lib/slaw/generator.rb', line 43

def reformat(text)
  @cleanser.reformat(text)
end