Class: GroffParser::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/groff_parser/engine.rb

Overview

A handy class who offers some methods to handle multiple documents at the same time

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ GroffParser::Engine

Stores a path, this one can be a path to a directory or to an specific file, use the proper class methods is up to you

Examples:

GroffParser::Engine.new("path/to/file")
GroffParser::Engine.new("path/to/directory")

Parameters:

  • path (String)

    the path to be stored

Since:

  • 0.1.0



26
27
28
# File 'lib/groff_parser/engine.rb', line 26

def initialize(path)
  @path = path || Dir.pwd
end

Instance Attribute Details

#pathObject

Since:

  • 0.1.0



11
12
13
# File 'lib/groff_parser/engine.rb', line 11

def path
  @path
end

Instance Method Details

#apply_to(*args) {|parse(args.join(" "))| ... } ⇒ Object

Executes a passed block, giving a GroffParser::Document as argument

Examples:

parser = GroffParser::Engine.new("/path/to/file")
parser.apply { |document| document.parse }

Yields:

  • (parse(args.join(" ")))

Since:

  • 0.1.0



86
87
88
# File 'lib/groff_parser/engine.rb', line 86

def apply_to(*args)
  yield parse(args.join(" "))
end

#apply_to_all(zipped = nil) ⇒ nil

Executes a passed block over all the documents in the current directory, giving a GroffParser::Document as argument for each block execution

Examples:

parser = GroffParser::Engine.new("/folder/with/some/files")
parser.apply_all { |document|  document.parse }

Parameters:

  • zipped (Symbol, Boolean, String) (defaults to: nil)

    indicates if the file is zipped or not (gzip)

Returns:

  • (nil)

Since:

  • 0.1.0



103
104
105
106
107
108
109
# File 'lib/groff_parser/engine.rb', line 103

def apply_to_all(zipped = nil)
  search_path = zipped ? "#{path}/*.gz" : "#{path}/*"

  Dir.glob(search_path) do |document|
    yield parse(document, zipped)
  end
end

#parse(document_path, zipped = nil) ⇒ GroffParser::Document

Parse a document located on a given path, if the document is contained inside of the ‘@path` variable you can pass only the document name otherwise it searchs for path provided as a full path

Examples:

parser = GroffParser::Engine.new("some/path")
parser.parse("path/to/another_file")
# Will parse another_file properly
parser.parse("some_file")
# Will search for a file called some_file in some/path

Parameters:

  • document_path (String)

    path for a document to parse

  • zipped (Symbol, Boolean, String) (defaults to: nil)

    indicates if the file is zipped or not (gzip)

Returns:

Since:

  • 0.1.0



49
50
51
52
53
# File 'lib/groff_parser/engine.rb', line 49

def parse(document_path, zipped = nil)
  dpath = document_path.include?(path) ? document_path : "#{path}/#{document_path}"

  Document.new(dpath, zipped)
end

#parse_all(zipped = nil) ⇒ Array<GroffParser::Document>

Parse all documents in a given directory, with a given format

Parameters:

  • format (Symbol, String)

    indicates the output format, could be: dvi, html, lbp, lj4, ps, ascii, cp1047, latin1, utf8, X75, X75, X100, X100 (default = utf8)

  • zipped (Symbol, Boolean, String) (defaults to: nil)

    indicates if the file is zipped or not (gzip)

Returns:

Since:

  • 0.1.0



67
68
69
70
71
72
73
74
75
76
# File 'lib/groff_parser/engine.rb', line 67

def parse_all(zipped = nil)
  documents   = []
  search_path = zipped ? "#{path}/*.gz" : "#{path}/*[0-9]"

  Dir.glob(search_path) do |document|
    documents << parse(document, zipped)
  end

  documents
end