Class: Agio::Bourse

Inherits:
Object
  • Object
show all
Extended by:
Flags
Defined in:
lib/agio/bourse.rb

Overview

The Bourse is where the incoming HTML document, after parsing through the Broker, will be transformed into Markdown.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Flags

extend_object

Constructor Details

#initialize(broker, agio) ⇒ Bourse

The Bourse is initialized with both the Broker instance and the Agio instance; the latter is used because it controls how some of the conversions should be performed.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/agio/bourse.rb', line 42

def initialize(broker, agio)
  @broker, @agio = broker, agio

  @formatter = Text::Format.new
  @formatter.first_indent = 0

  self.link_placement = nil
  @output = StringIO.new("")

  @abbr = { }
  @links = { }

  reset_flags(true)
end

Instance Attribute Details

#formatterObject (readonly)

An instance of Text::Format that is used to cleanly format the text output by the Bourse.



66
67
68
# File 'lib/agio/bourse.rb', line 66

def formatter
  @formatter
end

#outputObject (readonly)

The output StringIO.



70
71
72
# File 'lib/agio/bourse.rb', line 70

def output
  @output
end

Instance Method Details

#base_urlObject

:method: base_url? Returns true if the base URL has been set.



80
# File 'lib/agio/bourse.rb', line 80

string_flag :base_url, :public => true

#escape(string, parents) ⇒ Object



173
174
175
176
177
178
179
180
181
182
# File 'lib/agio/bourse.rb', line 173

def escape(string, parents)
  unless parents.include? "pre"
    string = string.
      gsub(/\*/) { "\\*" }.
      gsub(/`/) { "\\`" }.
      gsub(/_/) { "\\_" }.
      gsub(/^(\d+\. )/) { "\\$1" }
  end
  string
end

:method: decr_link_count Decrements the link count by the provided value.



27
# File 'lib/agio/bourse.rb', line 27

integer_flag  :link_count

:attr_reader: Controls how links are placed in the Markdown document.



103
104
105
# File 'lib/agio/bourse.rb', line 103

def link_placement
  @link_placement
end

:attr_writer: link_placement Controls how links are placed in the Markdown document.

In-Line

Links appear next to their wrapped text, like “[See the example](example.org/)”. The default for link_placement, set if the value is nil, :inline, or any other unrecognized value.

Paragraph

Links appear in the body as linked references, like “[See the example]”, and the reference “[1]: example.org” is placed immediately after the paragraph in which the link first appears. Used if the value of link_placement is :paragraph.

Endnote

Links appear in the body as linked references, like “[See the example]”, and the reference “[1]: example.org” is placed at the end of the document. Used if the value of link_placement is :endnote.



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/agio/bourse.rb', line 122

def link_placement=(value)
  value = case value
          when :inline, :paragraph, :endnote
            value
          when nil
            :inline
          else
            warn "Invalid value for link placement: #{value}; using inline."
            :inline
          end
  @link_placement = value
end

#list_stackObject

:method: list_stack? Returns true if the list_stack is not empty.



36
# File 'lib/agio/bourse.rb', line 36

array_flag    :list_stack

#skip_local_fragmentsObject

:method: skip_local_fragments? Returns true if local fragments are supposed to be skipped. See #skip_local_fragments.



98
# File 'lib/agio/bourse.rb', line 98

boolean_flag :skip_local_fragments, :public => true

#transformObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/agio/bourse.rb', line 141

def transform
  blocks = broker.blocks.map { |block|
    body = transform_block(block)

    if :paragraph == link_placement
      [ body, link_references ]
    else
      body
    end

  }.flatten.compact

  if :endnote == link_placement
    blocks << link_references
  end

  output.write(blocks.join("\n\n"))
end