Class: GroffParser::Document

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

Overview

A class representing a specific document to be parsed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, zipped = nil) ⇒ GroffParser::Document

Initializes the document class

Examples:

zipped_document = GroffParser::Document.new("path/to/file.gz", :zipped)
unzipped_document = GroffParser::Document.new("path/to/file.1")

Parameters:

  • path (String)

    the path where the document is located

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

    indicates if the document is zipped or not

Since:

  • 0.1.0



23
24
25
26
# File 'lib/groff_parser/document.rb', line 23

def initialize(path, zipped = nil)
  @path   = path
  @zipped = zipped
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/groff_parser/document.rb', line 7

def path
  @path
end

Instance Method Details

#formatted_content(format, flags = {}) ⇒ String

Content of the document in a especific format

Examples:

document.formatted_content(:html)

Parameters:

  • format (Symbol, String)

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

  • flags (Hash) (defaults to: {})

    hash containing flags in key-value format

Returns:

  • (String)

    the document content formated in the requested format

Since:

  • 0.1.0



100
101
102
# File 'lib/groff_parser/document.rb', line 100

def formatted_content(format, flags = {})
  `#{get} #{@path} | groff -mandoc -T#{format} #{formatted_flags(flags)}`
end

#groff(flags = {}) ⇒ Object



66
67
68
# File 'lib/groff_parser/document.rb', line 66

def groff(flags = {})
  `#{get} #{@path} | groff #{formatted_flags(flags)}`
end

#raw_content(flags = {}) ⇒ String

Raw content of the document, without being parsed, in pure groff format

Examples:

document.raw_content

Parameters:

  • flags (Hash) (defaults to: {})

    hash containing flags in key-value format

Returns:

  • (String)

    the document content in groff format

Since:

  • 0.1.0



82
83
84
# File 'lib/groff_parser/document.rb', line 82

def raw_content(flags = {})
  @raw_content ||= `#{get} #{@path} #{formatted_flags(flags)}`
end

#section(name) ⇒ String?

Currently in beta, given a section name it tries to search within the current document for a title passed as a parameter and return the contents within the title and the next one

Examples:

document.section("MY SECTION")
# searches for a section like this one:
# .SH
#  MY SECTION
#  ...

Parameters:

  • name (String, Symbol)

    name of the section

Returns:

  • (String, nil)

    the contents of the section or nil if the section doesn’t exist yet

Since:

  • 0.1.0



46
47
48
49
50
# File 'lib/groff_parser/document.rb', line 46

def section(name)
  raw_section = raw_content[/SH (?:\")?#{name}(?:\")?(.*?)SH/im]

  return raw_section.gsub("SH", "").gsub("#{name}", "") if raw_section
end