Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammetDilmac committed Jul 3, 2017
2 parents 09c937e + d2bcd0c commit 9b581c1
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
9 changes: 6 additions & 3 deletions lib/Html2Docx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ def self.create_docx(output, input)
def self.render(options = {})
initialization = Initialization.new(options)
options[:temp] = initialization.get_temp_directory
options[:initialization] = initialization

content_types = ContentTypes.new(options)
options[:content_types] = content_types

options[:main_relation] = true
relation = Relation.new(options)
options[:main_relation] = false
main_relation = Relation.new(options)
options[:main_relation] = main_relation

document = Document.new(options)
options[:document] = document

# Render
document.render
content_types.render
relation.render
main_relation.render

# Create Docx File
self.create_docx(options.fetch(:output), options.fetch(:temp))
Expand Down
3 changes: 2 additions & 1 deletion lib/Html2Docx/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def initialize(options = {})
@document = File.open(@document_file) { |f| Nokogiri::XML(f) }
@body = @document.at_xpath('//w:body')
@contents = []
@relation = options[:main_relation]

initial_body
add_html(options[:html])
Expand All @@ -25,7 +26,7 @@ def add_html(html)
case element.name
when 'p'
# Add paragraph
paragraph = DocumentObjects::Paragraph.new(@document, nil)
paragraph = DocumentObjects::Paragraph.new(@document, @relation)
paragraph.add_paragraph(element)
@contents.push paragraph.render
when /h[1-9]/
Expand Down
46 changes: 45 additions & 1 deletion lib/Html2Docx/document_objects/paragraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,22 @@ def add_paragraph(paragraph_object)
def create_paragraph(paragraph_object)
@paragraph = Nokogiri::XML::Node.new('w:p', @document)

paragraph_id = paragraph_object.attr('id')

add_paragraph_style paragraph_object
add_bookmark_start_tag(paragraph_id) if paragraph_id
add_paragraph_child paragraph_object.children
add_bookmark_end_tag(paragraph_id) if paragraph_id
end

def add_bookmark_start_tag(name)
bookmark_start_tag = @relation.create_internal_link_start_tag(name, @document)
@paragraph.add_child(bookmark_start_tag)
end

def add_bookmark_end_tag(name)
bookmark_end_tag = @relation.create_internal_link_end_tag(name, @document)
@paragraph.add_child(bookmark_end_tag)
end

def add_paragraph_style(paragraph_object)
Expand Down Expand Up @@ -100,7 +114,6 @@ def add_paragraph_child(children)
children.each do |child|
text_field = create_text_field
text_style = create_text_style

case child.name
when 'strong'
text_field.add_child add_strong_text(text_style)
Expand All @@ -113,9 +126,22 @@ def add_paragraph_child(children)
text_field.add_child add_underline_text(text_style)
when 's'
text_field.add_child add_stroke_text(text_style)
when 'a'
href = child.attr('href')
hyperlink_tag = create_hyperlink_tag(href)
text_field.add_child(add_link_class(text_style))
hyperlink_tag.add_child(text_field)
text_field.add_child add_paragraph_text(child.text)
hyperlink_tag.add_child text_field
@paragraph.add_child hyperlink_tag
next
end

paragraph_id = child.attr('id')

add_bookmark_start_tag(paragraph_id) if paragraph_id
text_field.add_child add_paragraph_text(child.text)
add_bookmark_end_tag(paragraph_id) if paragraph_id
@paragraph.add_child text_field
end
end
Expand All @@ -128,6 +154,16 @@ def create_text_style
Nokogiri::XML::Node.new('w:rPr', @document)
end

def create_hyperlink_tag(destination)
hyperlink_tag = Nokogiri::XML::Node.new('w:hyperlink', @document)

if destination.start_with?('#')
hyperlink_tag['w:anchor'] = destination.delete('#')
end

hyperlink_tag
end

def add_paragraph_text(value)
plain_text = Nokogiri::XML::Node.new('w:t', @document)
plain_text['xml:space'] = 'preserve'
Expand All @@ -136,6 +172,14 @@ def add_paragraph_text(value)
plain_text
end

def add_link_class(text_style)
r_style_tag = Nokogiri::XML::Node.new('w:rStyle', @document)
r_style_tag['w:val'] = 'Hyperlink'
text_style.add_child(r_style_tag)

text_style
end

def add_strong_text(text_style)
strong_text = Nokogiri::XML::Node.new('w:b', @document)
text_style.add_child(strong_text)
Expand Down
42 changes: 38 additions & 4 deletions lib/Html2Docx/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def initialize(options = {})
@relation = nil
@relations = []
@last_relation_id = 1
@internal_links = {}

if options[:main_relation]
@relation_file = File.join(options.fetch(:temp), 'word', '_rels', 'document2.xml.rels')
Expand Down Expand Up @@ -36,14 +37,47 @@ def add_relation(type, target)
@last_relation_id = @last_relation_id + 1
end

def render
File.open(@relation_file, 'w') { |f| f.write(Helpers::NokogiriHelper.to_xml(@relation)) }
end

def get_type(type)
end

def get_target(target)
end

def create_internal_link_start_tag(name, document)
bookmark_start_tag = Nokogiri::XML::Node.new('w:bookmarkStart', document)
bookmark_start_tag['w:id'] = create_internal_link_id(name)
bookmark_start_tag['w:name'] = name

bookmark_start_tag
end

def create_internal_link_end_tag(name, document)
bookmark_end_tag = Nokogiri::XML::Node.new('w:bookmarkEnd', document)
bookmark_end_tag['w:id'] = find_internal_link_id(name)

bookmark_end_tag
end

def create_internal_link_id(name)
id = find_internal_link_id(name)
if id
id = get_latest_internal_link_id + 1
@internal_links[id] = name
else
id
end
end

def get_latest_internal_link_id
@internal_links.keys.max || 0
end

def find_internal_link_id(name)
@internal_links.find{ |key, value| value == name }
end

def render
File.open(@relation_file, 'w') { |f| f.write(Helpers::NokogiriHelper.to_xml(@relation)) }
end
end
end
2 changes: 1 addition & 1 deletion lib/Html2Docx/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Html2Docx
VERSION = '0.2.1'
VERSION = '0.3.0'
end

0 comments on commit 9b581c1

Please sign in to comment.