parchment 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/parchment.rb +3 -0
- data/lib/parchment/document.rb +2 -2
- data/lib/parchment/formats/txt/document.rb +30 -0
- data/lib/parchment/formats/txt/paragraph.rb +23 -0
- data/lib/parchment/formats/txt/style.rb +12 -0
- data/lib/parchment/formats/txt/text_run.rb +11 -0
- data/lib/parchment/formats/txt/txt.rb +13 -0
- data/lib/parchment/paragraph.rb +3 -2
- data/lib/parchment/version.rb +2 -2
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b2b933353bca6915788888bf392d8c8515a3145
|
4
|
+
data.tar.gz: 1dc52ba73b73dd83a4fa7f7ada3ce7271b702a5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20503c349ede5f921df455d05e6367ac3ddfb205a9040054c550fce08de193298ed094ecf49fcd412fa17b9c8aedb6f429d4466ecab660d1bbd267b651f8bef3
|
7
|
+
data.tar.gz: e19cff1bbdebce5d854c5bed607ddb610eaf19227a09865b3f3512910d5159c1a9b590446a48f79bcf36620c0907218795e1aab67395835f40302d0a50f7bdf7
|
data/README.md
CHANGED
@@ -4,6 +4,21 @@ Parchment is a simple, flexible library for interacting with word processing
|
|
4
4
|
document files. Initially intended for outputting .docx and .odt files as
|
5
5
|
HTML fragments, it is built to be flexible for future use.
|
6
6
|
|
7
|
+
## Why?
|
8
|
+
|
9
|
+
You may ask, is there really a need for another document parsing library?
|
10
|
+
Apparently so.
|
11
|
+
|
12
|
+
I had a seemingly simple task I wanted to do: Take a Microsoft Word or Open
|
13
|
+
Office Writer file and display it as an HTML fragment within a larger page,
|
14
|
+
with as much formatting as necessary. I couldn't find a decent solution for
|
15
|
+
this, especially from within a Rails app. Most other libraries are focused
|
16
|
+
on converting whole documents, such as a Word file into a complete
|
17
|
+
fully-compliant HTML page.
|
18
|
+
|
19
|
+
I wanted the simplicity of what is seen under usage below, without
|
20
|
+
patching together several different command-line tools. That's Parchment.
|
21
|
+
|
7
22
|
## Installation
|
8
23
|
|
9
24
|
Add this line to your application's Gemfile:
|
data/lib/parchment.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'parchment/document'
|
2
2
|
require 'parchment/formats/odt/odt'
|
3
3
|
require 'parchment/formats/docx/docx'
|
4
|
+
require 'parchment/formats/txt/txt'
|
4
5
|
|
5
6
|
module Parchment
|
6
7
|
|
@@ -14,6 +15,8 @@ module Parchment
|
|
14
15
|
Parchment::ODT.read(path)
|
15
16
|
when 'docx'
|
16
17
|
Parchment::DOCX.read(path)
|
18
|
+
when 'txt'
|
19
|
+
Parchment::TXT.read(path)
|
17
20
|
else
|
18
21
|
raise UnsupportedFileFormatError, 'File format is not supported.'
|
19
22
|
end
|
data/lib/parchment/document.rb
CHANGED
@@ -52,7 +52,7 @@ module Parchment
|
|
52
52
|
# Output entire document as a HTML fragment String.
|
53
53
|
#
|
54
54
|
def to_html
|
55
|
-
paragraphs.map(&:to_html).join(
|
55
|
+
paragraphs.map(&:to_html).join("\n")
|
56
56
|
end
|
57
57
|
|
58
58
|
private
|
@@ -74,4 +74,4 @@ module Parchment
|
|
74
74
|
raise MissingFormatterMethodError
|
75
75
|
end
|
76
76
|
end
|
77
|
-
end
|
77
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'parchment/document'
|
2
|
+
require_relative 'paragraph'
|
3
|
+
require_relative 'style'
|
4
|
+
|
5
|
+
module Parchment
|
6
|
+
module TXT
|
7
|
+
class Document < Parchment::Document
|
8
|
+
|
9
|
+
def initialize(file)
|
10
|
+
@text = file
|
11
|
+
@styles = []
|
12
|
+
set_paragraphs
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
# These methods parse and add the Document's children and defaults.
|
18
|
+
#
|
19
|
+
def set_paragraphs
|
20
|
+
set_default_paragraph_style
|
21
|
+
# double quotes matter here
|
22
|
+
@paragraphs = @text.gsub("\r", '').split("\n").map { |line| Parchment::TXT::Paragraph.new(line, self) }
|
23
|
+
end
|
24
|
+
|
25
|
+
def set_default_paragraph_style
|
26
|
+
@default_paragraph_style = Parchment::TXT::Style.new
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'parchment/paragraph'
|
2
|
+
require_relative 'style'
|
3
|
+
require_relative 'text_run'
|
4
|
+
|
5
|
+
module Parchment
|
6
|
+
module TXT
|
7
|
+
class Paragraph < Parchment::Paragraph
|
8
|
+
|
9
|
+
def initialize(line, document)
|
10
|
+
@node = line # superlass is expecting @node, so we give it @node
|
11
|
+
@document = document
|
12
|
+
@style = @document.default_paragraph_style
|
13
|
+
super()
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def set_text_runs
|
19
|
+
@text_runs = [Parchment::TXT::TextRun.new(@node, self, @document)]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/parchment/paragraph.rb
CHANGED
@@ -44,7 +44,7 @@ module Parchment
|
|
44
44
|
# Output the unformatted Paragraph's content as a String.
|
45
45
|
#
|
46
46
|
def to_s
|
47
|
-
@
|
47
|
+
@text_runs.map(&:to_s).join('')
|
48
48
|
end
|
49
49
|
alias :text :to_s
|
50
50
|
|
@@ -54,7 +54,8 @@ module Parchment
|
|
54
54
|
def to_html
|
55
55
|
html = ''
|
56
56
|
text_runs.each { |text_run| html << text_run.to_html }
|
57
|
-
styles = {
|
57
|
+
styles = {}
|
58
|
+
styles['font-size'] = "#{font_size}pt" unless font_size.nil?
|
58
59
|
styles['text-align'] = @style.text_align unless @style.aligned_left?
|
59
60
|
html_tag(:p, content: html, styles: styles)
|
60
61
|
end
|
data/lib/parchment/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module Parchment
|
2
|
-
VERSION = '0.0.
|
3
|
-
end
|
2
|
+
VERSION = '0.0.2'
|
3
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parchment
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Allen Petlock
|
@@ -95,6 +95,11 @@ files:
|
|
95
95
|
- lib/parchment/formats/docx/document.rb
|
96
96
|
- lib/parchment/formats/docx/style.rb
|
97
97
|
- lib/parchment/formats/docx/paragraph.rb
|
98
|
+
- lib/parchment/formats/txt/text_run.rb
|
99
|
+
- lib/parchment/formats/txt/document.rb
|
100
|
+
- lib/parchment/formats/txt/style.rb
|
101
|
+
- lib/parchment/formats/txt/txt.rb
|
102
|
+
- lib/parchment/formats/txt/paragraph.rb
|
98
103
|
- lib/parchment/formats/odt/text_run.rb
|
99
104
|
- lib/parchment/formats/odt/document.rb
|
100
105
|
- lib/parchment/formats/odt/style.rb
|
@@ -121,7 +126,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
121
126
|
requirements:
|
122
127
|
- - '>='
|
123
128
|
- !ruby/object:Gem::Version
|
124
|
-
version:
|
129
|
+
version: 1.9.3
|
125
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
131
|
requirements:
|
127
132
|
- - '>='
|