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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0d6c2118023a45879f6d70a6a9504da8af5e2942
4
- data.tar.gz: a2a64bf4e2d264dd56f003a066fa027df6b0c911
3
+ metadata.gz: 2b2b933353bca6915788888bf392d8c8515a3145
4
+ data.tar.gz: 1dc52ba73b73dd83a4fa7f7ada3ce7271b702a5a
5
5
  SHA512:
6
- metadata.gz: 92165f1b6633f8ae88545b415b5c53dddb532d0a9f9f84860dbc1e64cc381d84c2ac520da87e10a6a09d6e361dbb0b38c052f60ab6f7a3f1b2ae31590ffb79b5
7
- data.tar.gz: d8d175ba3eff1d82cb5bf45680bfd498e1d7b362fb021074fae9b166f904bec55d91430e31cbbbdf8b2064ab0d7e27e0537f2e535adee0659cf5c4881735920b
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:
@@ -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
@@ -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('\n')
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
@@ -0,0 +1,12 @@
1
+ require 'parchment/style'
2
+
3
+ module Parchment
4
+ module TXT
5
+ class Style < Parchment::Style
6
+
7
+ def initialize
8
+ # Where this thing is at, it doesn't need styles.
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ module Parchment
2
+ module TXT
3
+ class TextRun < Parchment::TextRun
4
+
5
+ def initialize(line, paragraph, document)
6
+ @content = @node = line # superclass expects @node, so we give it @node
7
+ @style = paragraph.style
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ require 'zip'
2
+ require_relative 'document'
3
+
4
+ module Parchment
5
+
6
+ # = Parchment plain text format parser
7
+ #
8
+ module TXT
9
+ def self.read(path)
10
+ Parchment::TXT::Document.new(File.read(path))
11
+ end
12
+ end
13
+ end
@@ -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
- @node.content
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 = { 'font-size' => "#{font_size}pt" }
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
@@ -1,3 +1,3 @@
1
1
  module Parchment
2
- VERSION = '0.0.1'
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.1
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: '0'
129
+ version: 1.9.3
125
130
  required_rubygems_version: !ruby/object:Gem::Requirement
126
131
  requirements:
127
132
  - - '>='