Skip to content

Commit

Permalink
Use nokogiri's HTML5 parser for text nodes (#129)
Browse files Browse the repository at this point in the history
* use nokogiri's HTML5 parser for text nodes

* bump up version
  • Loading branch information
mauriciomenegaz authored Mar 21, 2024
1 parent 9a68b0f commit 66f2633
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- None

## 0.8.0

### Dependencies

- nokogiri >= 1.12.0 (was >= 1.10.0)

## 0.7.3

### Fixed
Expand Down
7 changes: 3 additions & 4 deletions lib/odf-report/parser/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ def initialize(text, template_node)
end

def parse
html = Nokogiri::HTML5.fragment(@text)

xml = @template_node.parse(@text)

xml.css("p", "h1", "h2").each do |p|

html.css("p", "h1", "h2").each do |p|
style = check_style(p)
text = parse_formatting(p.inner_html)

Expand All @@ -63,6 +61,7 @@ def parse_formatting(text)
text.gsub!(/<strong.*?>(.+?)<\/strong>/) { "<text:span text:style-name=\"bold\">#{$1}<\/text:span>" }
text.gsub!(/<em.*?>(.+?)<\/em>/) { "<text:span text:style-name=\"italic\">#{$1}<\/text:span>" }
text.gsub!(/<u.*?>(.+?)<\/u>/) { "<text:span text:style-name=\"underline\">#{$1}<\/text:span>" }
text.gsub!("<br\/?>", "<text:line-break/>")
text.gsub!("\n", "")
text
end
Expand Down
2 changes: 1 addition & 1 deletion lib/odf-report/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ODFReport
VERSION = "0.7.3"
VERSION = "0.8.0"
end
2 changes: 1 addition & 1 deletion odf-report.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "launchy"

s.add_runtime_dependency('rubyzip', ">= 1.3.0")
s.add_runtime_dependency('nokogiri', ">= 1.10.0")
s.add_runtime_dependency('nokogiri', ">= 1.12.0")
s.add_runtime_dependency('mime-types')

end
Binary file modified spec/templates/specs.odt
Binary file not shown.
48 changes: 48 additions & 0 deletions spec/texts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
RSpec.describe "Texts" do
before(:context) do

@text1 = <<-HTML
<p>This is some text in a paragraph</p>
HTML

@text2 = <<-HTML
<p>Text before line break <br> text after line break</p>
HTML

@text3 = <<-HTML
<p>Text before entities
Maur&iacute;cio
Text after entities</p>
HTML


report = ODFReport::Report.new("spec/templates/specs.odt") do |r|

r.add_text(:text1, @text1)
r.add_text(:text2, @text2)
r.add_text(:text3, @text3)

end

report.generate("spec/result/specs.odt")

@data = Inspector.new("spec/result/specs.odt")

end

it "simple text replacement" do
expect(@data.text).to include "This is some text in a paragraph"
end

it "text replacement with <br>" do
expect(@data.text).to include "Text before line break"
expect(@data.text).to include "text after line break"
end

it "text replacement with html entities" do
expect(@data.text).to include "Text before entities"
expect(@data.text).to include "Maurício"
expect(@data.text).to include "Text after entities"
end

end

0 comments on commit 66f2633

Please sign in to comment.