groff_parser 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/.travis.yml +10 -0
- data/README.md +10 -10
- data/lib/groff_parser/document.rb +48 -8
- data/lib/groff_parser/engine.rb +11 -11
- data/lib/groff_parser/version.rb +1 -1
- data/test/document_test.rb +30 -3
- data/test/engine_test.rb +10 -8
- metadata +14 -13
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MzlhMjU3N2MzNTMyNzMyMDZhMTIwMmFkN2RlNTY1NGNjYjRhNTdjOQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YTFkYzdkYzgyYmQ0Y2RkNjYyZjg0YTliNmRkNmJiYTA1YzYwNDhlYw==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
M2MxZTY4YjE5ZDY4NWZkMGM1YmI0MTJmOTJjYWY3YWQ5MThmZGEzNzE1ODNm
|
10
|
+
YjhlZDY0MTgxZjU0ZjE1Y2Q0NTBhYzM3MDhkMTIzMDRjNDkyMjM3ZGRlNDdh
|
11
|
+
NjM5MjRiMDI2MzFkN2RjMGZiNDQ3OWE5MzkwZDEzODViZTZiNDE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YjdhMTBjMzY1MjU2MmI0ZmJjN2I3MmZkZjM3YzhlZWIyODUyMTU3ZWU0Yzc2
|
14
|
+
YjQ4Nzk5NDNkMmVlZTAxM2JjNGIwNjNmYTVlNzM5ODYxYTU0YzI4ZDg1OGMy
|
15
|
+
YjAxMWQxMzA0ZTVkYTk3NmQ1MzdhZGQyYTAyNGEyZjlhMDhmNmY=
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -2,28 +2,28 @@
|
|
2
2
|
|
3
3
|
### Status
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/groff_parser.png)](http://badge.fury.io/rb/groff_parser)
|
5
|
+
[![Build Status](https://secure.travis-ci.org/roperzh/groff_parser.png?branch=master)](http://travis-ci.org/roperzh/groff_parser?branch=master)
|
5
6
|
[![Code Climate](https://codeclimate.com/github/roperzh/groff_parser.png)](https://codeclimate.com/github/roperzh/groff_parser)
|
6
7
|
|
7
8
|
Tiny library to parse groff files, with some handy metods to manage directories with a lot of files
|
8
9
|
|
9
10
|
## Installation
|
10
11
|
|
11
|
-
|
12
|
+
This gem relies on the [`groff`](http://savannah.gnu.org/projects/groff) GNU package, so in order to work you will need to install it.
|
12
13
|
|
13
|
-
|
14
|
+
```bash
|
15
|
+
apt-get install groff
|
16
|
+
```
|
14
17
|
|
15
|
-
|
18
|
+
If you already have the `groff` package, just install the gem as usual:
|
16
19
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
$ gem install groff_parser
|
20
|
+
```bash
|
21
|
+
$ gem install groff_parser
|
22
|
+
```
|
22
23
|
|
23
24
|
## Basic Usage
|
24
25
|
|
25
|
-
|
26
|
-
|
26
|
+
For a detailed information about how the gem works internally, you can take a look at the [`docs`](http://rubydoc.info/gems/groff_parser)
|
27
27
|
|
28
28
|
## Contributing
|
29
29
|
|
@@ -11,16 +11,16 @@ module GroffParser
|
|
11
11
|
# @since 0.1.0
|
12
12
|
#
|
13
13
|
# @example
|
14
|
-
# zipped_document = GroffParser::Document.new("path/to/file.gz", zipped
|
15
|
-
# unzipped_document = GroffParser::Document.new("path/to/file.1"
|
14
|
+
# zipped_document = GroffParser::Document.new("path/to/file.gz", :zipped)
|
15
|
+
# unzipped_document = GroffParser::Document.new("path/to/file.1")
|
16
16
|
#
|
17
17
|
# @param path [String] the path where the document is located
|
18
18
|
#
|
19
|
-
# @param zipped [Boolean] indicates if the document is zipped or not
|
19
|
+
# @param zipped [Symbol, Boolean, String] indicates if the document is zipped or not
|
20
20
|
#
|
21
21
|
# @return [GroffParser::Document] a new instance of a Document class
|
22
22
|
|
23
|
-
def initialize(path, zipped
|
23
|
+
def initialize(path, zipped = nil)
|
24
24
|
@path = path
|
25
25
|
@zipped = zipped
|
26
26
|
end
|
@@ -49,6 +49,24 @@ module GroffParser
|
|
49
49
|
return raw_section.gsub("SH", "").gsub("#{name}", "") if raw_section
|
50
50
|
end
|
51
51
|
|
52
|
+
# Executes the groff command, with a given set of flags, and returns the
|
53
|
+
# output
|
54
|
+
#
|
55
|
+
# @since 0.4.0
|
56
|
+
#
|
57
|
+
# @example
|
58
|
+
# document.groff(m: :mandoc)
|
59
|
+
# # will execute: `groff -m mandoc
|
60
|
+
#
|
61
|
+
# @param flags [Hash] hash containing flags in key-value format
|
62
|
+
#
|
63
|
+
# @return [String] the result of the command
|
64
|
+
|
65
|
+
|
66
|
+
def groff(flags = {})
|
67
|
+
`#{get} #{@path} | groff #{formatted_flags(flags)}`
|
68
|
+
end
|
69
|
+
|
52
70
|
# Raw content of the document, without being parsed, in pure
|
53
71
|
# groff format
|
54
72
|
#
|
@@ -57,10 +75,12 @@ module GroffParser
|
|
57
75
|
# @example
|
58
76
|
# document.raw_content
|
59
77
|
#
|
78
|
+
# @param flags [Hash] hash containing flags in key-value format
|
79
|
+
#
|
60
80
|
# @return [String] the document content in groff format
|
61
81
|
|
62
|
-
def raw_content
|
63
|
-
@raw_content ||= `#{get} #{@path}`
|
82
|
+
def raw_content(flags = {})
|
83
|
+
@raw_content ||= `#{get} #{@path} #{formatted_flags(flags)}`
|
64
84
|
end
|
65
85
|
|
66
86
|
# Content of the document in a especific format
|
@@ -73,10 +93,12 @@ module GroffParser
|
|
73
93
|
# @param format [Symbol, String] indicates the output format, could be:
|
74
94
|
# dvi, html, lbp, lj4, ps, ascii, cp1047, latin1, utf8, X75, X75, X100, X100
|
75
95
|
#
|
96
|
+
# @param flags [Hash] hash containing flags in key-value format
|
97
|
+
#
|
76
98
|
# @return [String] the document content formated in the requested format
|
77
99
|
|
78
|
-
def formatted_content(format)
|
79
|
-
`#{get} #{@path} | groff -mandoc -T#{format}`
|
100
|
+
def formatted_content(format, flags = {})
|
101
|
+
`#{get} #{@path} | groff -mandoc -T#{format} #{formatted_flags(flags)}`
|
80
102
|
end
|
81
103
|
|
82
104
|
private
|
@@ -98,5 +120,23 @@ module GroffParser
|
|
98
120
|
def get
|
99
121
|
@zipped ? "zcat" : "cat"
|
100
122
|
end
|
123
|
+
|
124
|
+
# Helper to reduce a hash containing flags to a single line string
|
125
|
+
# according to the usual GNU convention
|
126
|
+
#
|
127
|
+
# @since 0.4.0
|
128
|
+
#
|
129
|
+
# @example
|
130
|
+
# formatted_flags(m: :mandoc) # => "-mmandoc"
|
131
|
+
#
|
132
|
+
# @params flags [Hash] flags to be reduced
|
133
|
+
#
|
134
|
+
# @return [String] string with the parsed flags following the usual GNU convention
|
135
|
+
|
136
|
+
def formatted_flags(flags)
|
137
|
+
flags.reduce("") {
|
138
|
+
|result, flag| result.concat(" -#{flag[0]}#{flag[1]}")
|
139
|
+
}
|
140
|
+
end
|
101
141
|
end
|
102
142
|
end
|
data/lib/groff_parser/engine.rb
CHANGED
@@ -23,8 +23,8 @@ module GroffParser
|
|
23
23
|
#
|
24
24
|
# @return [GroffParser::Engine] a new Engine instance
|
25
25
|
|
26
|
-
def initialize(path
|
27
|
-
@path = path
|
26
|
+
def initialize(path)
|
27
|
+
@path = path || Dir.pwd
|
28
28
|
end
|
29
29
|
|
30
30
|
# Parse a document located on a given path, if the document is contained
|
@@ -42,14 +42,14 @@ module GroffParser
|
|
42
42
|
#
|
43
43
|
# @param document_path [String] path for a document to parse
|
44
44
|
#
|
45
|
-
# @param zipped [Boolean] indicates if the file is zipped or not (gzip)
|
45
|
+
# @param zipped [Symbol, Boolean, String] indicates if the file is zipped or not (gzip)
|
46
46
|
#
|
47
47
|
# @return [GroffParser::Document] the document, parsed and ready to be manipulated
|
48
48
|
|
49
|
-
def parse(document_path, zipped
|
49
|
+
def parse(document_path, zipped = nil)
|
50
50
|
dpath = document_path.include?(path) ? document_path : "#{path}/#{document_path}"
|
51
51
|
|
52
|
-
Document.new(dpath, zipped
|
52
|
+
Document.new(dpath, zipped)
|
53
53
|
end
|
54
54
|
|
55
55
|
# Parse all documents in a given directory, with a given format
|
@@ -60,16 +60,16 @@ module GroffParser
|
|
60
60
|
# dvi, html, lbp, lj4, ps, ascii, cp1047, latin1, utf8, X75, X75, X100, X100
|
61
61
|
# (default = utf8)
|
62
62
|
#
|
63
|
-
# @param zipped [Boolean] indicates if the file is zipped or not (gzip)
|
63
|
+
# @param zipped [Symbol, Boolean, String] indicates if the file is zipped or not (gzip)
|
64
64
|
#
|
65
65
|
# @return [Array<GroffParser::Document>] an array of all the parsed documents
|
66
66
|
|
67
|
-
def parse_all(
|
67
|
+
def parse_all(zipped = nil)
|
68
68
|
documents = []
|
69
69
|
search_path = zipped ? "#{path}/*.gz" : "#{path}/*[0-9]"
|
70
70
|
|
71
71
|
Dir.glob(search_path) do |document|
|
72
|
-
documents << parse(document, zipped
|
72
|
+
documents << parse(document, zipped)
|
73
73
|
end
|
74
74
|
|
75
75
|
documents
|
@@ -96,15 +96,15 @@ module GroffParser
|
|
96
96
|
# parser = GroffParser::Engine.new("/folder/with/some/files")
|
97
97
|
# parser.apply_all { |document| document.parse }
|
98
98
|
#
|
99
|
-
# @param zipped [Boolean] indicates if the file is zipped or not (gzip)
|
99
|
+
# @param zipped [Symbol, Boolean, String] indicates if the file is zipped or not (gzip)
|
100
100
|
#
|
101
101
|
# @return [nil]
|
102
102
|
|
103
|
-
def apply_to_all(zipped
|
103
|
+
def apply_to_all(zipped = nil)
|
104
104
|
search_path = zipped ? "#{path}/*.gz" : "#{path}/*"
|
105
105
|
|
106
106
|
Dir.glob(search_path) do |document|
|
107
|
-
yield parse(document, zipped
|
107
|
+
yield parse(document, zipped)
|
108
108
|
end
|
109
109
|
end
|
110
110
|
end
|
data/lib/groff_parser/version.rb
CHANGED
data/test/document_test.rb
CHANGED
@@ -3,13 +3,15 @@ require File.expand_path(File.join("test/test_helper"))
|
|
3
3
|
describe GroffParser::Document do
|
4
4
|
|
5
5
|
let(:zipped_document) {
|
6
|
-
GroffParser::Document.new("test/fixtures/git.1.gz", zipped
|
6
|
+
GroffParser::Document.new("test/fixtures/git.1.gz", :zipped)
|
7
7
|
}
|
8
8
|
|
9
9
|
let(:unzipped_document) {
|
10
|
-
GroffParser::Document.new("test/fixtures/git.1"
|
10
|
+
GroffParser::Document.new("test/fixtures/git.1")
|
11
11
|
}
|
12
12
|
|
13
|
+
let(:timestamp) { Regexp.new(/\<\!\-\- CreationDate\: (.*?) \-\-\>/) }
|
14
|
+
|
13
15
|
describe "#section" do
|
14
16
|
it "returns the contents of a section delimited by a given title" do
|
15
17
|
zipped_document.section("NAME").must_equal(
|
@@ -49,7 +51,6 @@ describe GroffParser::Document do
|
|
49
51
|
end
|
50
52
|
|
51
53
|
describe "#formatted_content" do
|
52
|
-
let(:timestamp) { Regexp.new(/\<\!\-\- CreationDate\: (.*?) \-\-\>/) }
|
53
54
|
|
54
55
|
it "returns the content in the requested format" do
|
55
56
|
# Due to a small delay parsing data, we need to supress the timestamps
|
@@ -74,4 +75,30 @@ describe GroffParser::Document do
|
|
74
75
|
)
|
75
76
|
end
|
76
77
|
end
|
78
|
+
|
79
|
+
describe "#groff" do
|
80
|
+
context "given a set of flags" do
|
81
|
+
it "executes the groff command with the flags provided" do
|
82
|
+
zipped_document.groff(:T => :utf8, :m => :mandoc).must_equal(
|
83
|
+
`zcat test/fixtures/git.1.gz | groff -Tutf8 -mmandoc`
|
84
|
+
)
|
85
|
+
|
86
|
+
zipped_document.groff(:T => :utf8, :m => :mandoc).must_equal(
|
87
|
+
`cat test/fixtures/git.1 | groff -Tutf8 -mmandoc`
|
88
|
+
)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context "without flags" do
|
93
|
+
it "executes groff command without any aditional options" do
|
94
|
+
zipped_document.groff.gsub(timestamp, "").must_equal(
|
95
|
+
`zcat test/fixtures/git.1.gz | groff`.gsub(timestamp, "")
|
96
|
+
)
|
97
|
+
|
98
|
+
zipped_document.groff.gsub(timestamp, "").must_equal(
|
99
|
+
`cat test/fixtures/git.1 | groff`.gsub(timestamp, "")
|
100
|
+
)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
77
104
|
end
|
data/test/engine_test.rb
CHANGED
@@ -2,19 +2,19 @@ require File.expand_path(File.join("test/test_helper"))
|
|
2
2
|
|
3
3
|
describe GroffParser::Engine do
|
4
4
|
|
5
|
-
let(:engine) { GroffParser::Engine.new(
|
5
|
+
let(:engine) { GroffParser::Engine.new("test/fixtures") }
|
6
6
|
|
7
7
|
describe "#parse" do
|
8
8
|
context "given a zipped document" do
|
9
9
|
it "returns a new GroffParser::Document instance" do
|
10
|
-
engine.parse("git.1.gz", zipped
|
10
|
+
engine.parse("git.1.gz", :zipped).class.must_equal(
|
11
11
|
GroffParser::Document
|
12
12
|
)
|
13
13
|
end
|
14
14
|
|
15
15
|
it "allows to retrieve information of the parsed file" do
|
16
|
-
engine.parse("git.1.gz", zipped
|
17
|
-
GroffParser::Document.new("test/fixtures/git.1.gz", zipped
|
16
|
+
engine.parse("git.1.gz", :zipped).raw_content.must_equal(
|
17
|
+
GroffParser::Document.new("test/fixtures/git.1.gz", :zipped)
|
18
18
|
.raw_content
|
19
19
|
)
|
20
20
|
end
|
@@ -29,7 +29,7 @@ describe GroffParser::Engine do
|
|
29
29
|
|
30
30
|
it "allows to retrieve information of the parsed file" do
|
31
31
|
engine.parse("git.1").raw_content.must_equal(
|
32
|
-
GroffParser::Document.new("test/fixtures/git.1.gz", zipped
|
32
|
+
GroffParser::Document.new("test/fixtures/git.1.gz", :zipped)
|
33
33
|
.raw_content
|
34
34
|
)
|
35
35
|
end
|
@@ -39,9 +39,11 @@ describe GroffParser::Engine do
|
|
39
39
|
|
40
40
|
describe "#parse_all" do
|
41
41
|
it "returns an array with all the documents contents present in the path" do
|
42
|
-
engine.parse_all
|
43
|
-
|
44
|
-
)
|
42
|
+
documents = engine.parse_all
|
43
|
+
|
44
|
+
documents.length.must_equal(1)
|
45
|
+
documents.first.class.must_equal(GroffParser::Document)
|
46
|
+
documents.first.path.must_equal("test/fixtures/git.1")
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|
metadata
CHANGED
@@ -1,69 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: groff_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roberto Dip
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ! '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ! '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ! '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ! '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '5'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: inch
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ! '>='
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
description: Tiny library to parse groff files, with some handy metods to manage directories
|
@@ -74,7 +74,8 @@ executables: []
|
|
74
74
|
extensions: []
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
|
-
-
|
77
|
+
- .gitignore
|
78
|
+
- .travis.yml
|
78
79
|
- Gemfile
|
79
80
|
- LICENSE.txt
|
80
81
|
- README.md
|
@@ -99,12 +100,12 @@ require_paths:
|
|
99
100
|
- lib
|
100
101
|
required_ruby_version: !ruby/object:Gem::Requirement
|
101
102
|
requirements:
|
102
|
-
- -
|
103
|
+
- - ! '>='
|
103
104
|
- !ruby/object:Gem::Version
|
104
105
|
version: '0'
|
105
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
107
|
requirements:
|
107
|
-
- -
|
108
|
+
- - ! '>='
|
108
109
|
- !ruby/object:Gem::Version
|
109
110
|
version: '0'
|
110
111
|
requirements: []
|