Class: Agio

Inherits:
Object
  • Object
show all
Defined in:
lib/agio.rb,
lib/agio.rb

Overview

Agio

Agio converts HTML to Markdown.

About the Name

The name was chosen because agio is “a premium on money in exchange”, sort of the opposite of a markdown. It comes from the Italian aggio (premium), not from the Italian agio (ease), although the hope is that there is an ease in use of this library.

It is structurally based on Aaron Swarz’s html2txt Python script inasmuch as the SAX parsing he does is also done here and with pretty much the same behaviour.

License

This code is licensed under MIT License

Defined Under Namespace

Modules: Flags Classes: Block, Bourse, Broker, CData, Comment, Data, HTMLElementDescription, XMLDecl

Constant Summary collapse

VERSION =
"0.5.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Agio

Returns a new instance of Agio.

Yields:

  • (_self)

Yield Parameters:

  • _self (Agio)

    the object that the method was called on



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/agio.rb', line 129

def initialize(options = {})
  @broker = Agio::Broker.new
  @bourse = Agio::Bourse.new(broker, self)

  self.html           = options[:html]
  self.columns        = options[:columns]
  self.link_placement = options[:link_placement]

  yield self if block_given?

  @parser = Nokogiri::HTML::SAX::Parser.new(broker)
end

Instance Attribute Details

#htmlObject

:attr_accessor: The default HTML document to be processed. Because the #parse method can be called with an HTML document, this does not need to be set.



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

def html
  @html
end

Class Method Details

.to_markdown(html) ⇒ Object



160
161
162
# File 'lib/agio.rb', line 160

def self.to_markdown(html)
  self.new.to_markdown(html)
end

Instance Method Details

#base_urlObject

:attr_reader: base_url The base URL for implicit (or local) link references. If not provided, links will remain implicit. This is a String value.



88
89
90
# File 'lib/agio.rb', line 88

def base_url
  bourse.base_url
end

#base_url=(value) ⇒ Object

:attr_writer: base_url The base URL for implicit (or local) link references. If not provided, links will remain implicit. This is a String value.



95
96
97
# File 'lib/agio.rb', line 95

def base_url=(value)
  bourse.base_url = value
end

#columnsObject

:attr_reader: The width of the body text for the generated Markdown text outside of pre bodies and other items which do not wrap well in most Markdown parsers.



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

def columns
  bourse.formatter.columns
end

#columns=(value) ⇒ Object

:attr_writer: The width of the body text for the generated Markdown text outside of pre bodies and other items which do not wrap well in most Markdown parsers.

If nil is provided, the default value of 78 is set.



53
54
55
56
# File 'lib/agio.rb', line 53

def columns=(value)
  bourse.formatter.columns = value || 78
  bourse.formatter.columns
end

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



61
62
63
# File 'lib/agio.rb', line 61

def link_placement
  bourse.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.



80
81
82
# File 'lib/agio.rb', line 80

def link_placement=(value)
  bourse.link_placement = value
end

#skip_local_fragmentsObject

:attr_reader: skip_local_fragments Controls whether local link references containing fragments will be output in the final document.

A local link reference is either an implicit link reference (one missing the protocol and host, such as ‘<a href=“about.html”>’ or ‘<a href=“/about.html”>’) or one that points to the #base_url.

If this value is true, links that refer to fragments on local URIs will be ignored (such as ‘<a href=“about.html#address”>’).



110
111
112
# File 'lib/agio.rb', line 110

def skip_local_fragments
  bourse.skip_local_fragments
end

#skip_local_fragments=(value) ⇒ Object

:attr_writer: skip_local_fragments Controls whether local link references containing fragments will be output in the final document.

A local link reference is either an implicit link reference (one missing the protocol and host, such as ‘<a href=“about.html”>’ or ‘<a href=“/about.html”>’) or one that points to the #base_url.

If this value is true, links that refer to fragments on local URIs will be ignored (such as ‘<a href=“about.html#address”>’).



125
126
127
# File 'lib/agio.rb', line 125

def skip_local_fragments=(value)
  bourse.skip_local_fragments = value
end

#to_s(html = nil) ⇒ Object Also known as: to_markdown



154
155
156
157
# File 'lib/agio.rb', line 154

def to_s(html = nil)
  transform(html || self.html)
  bourse.output.string
end