Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammetDilmac committed Jun 22, 2017
2 parents a5801fe + f843923 commit 60811c2
Show file tree
Hide file tree
Showing 29 changed files with 69 additions and 53 deletions.
7 changes: 2 additions & 5 deletions Html2Docx.gemspec
Expand Up @@ -9,11 +9,8 @@ Gem::Specification.new do |spec|
spec.authors = ['MuhammetDilmac']
spec.email = ['iletisim@muhammetdilmac.com.tr']

spec.summary = 'HTML çıktısından Docx oluşturmayı sağlayan ruby' \
'kütüphanesi'
spec.description = 'Kendisine özel olarak oluşturulan html çıktısını ' \
'işleyerek bu çıktıdan Docx üretmeyi sağlayan ruby ' \
'kütüphanesi'
spec.summary = 'Ruby library for creating Docx from HTML output'
spec.description = 'Ruby library for creating Docx from HTML output'
spec.homepage = 'https://www.github.com/MuhammetDilmac/Html2Docx'
spec.license = 'MIT'

Expand Down
2 changes: 2 additions & 0 deletions lib/Html2Docx.rb
Expand Up @@ -11,7 +11,9 @@
require 'Html2Docx/content_types'
require 'Html2Docx/relation'
require 'Html2Docx/document'

require 'Html2Docx/document_objects/paragraph'
require 'Html2Docx/document_objects/heading'

module Html2Docx
ROOT_PATH = File.expand_path(File.join(File.dirname(__FILE__), '../'))
Expand Down
4 changes: 4 additions & 0 deletions lib/Html2Docx/document.rb
Expand Up @@ -28,6 +28,10 @@ def add_html(html)
paragraph = DocumentObjects::Paragraph.new(@document, nil)
paragraph.add_paragraph(element)
@contents.push paragraph.render
when /h[1-9]/
heading = DocumentObjects::Heading.new(@document)
heading.add_heading(element)
@contents.push heading.render
when 'table'
# Add table
@contents.push ''
Expand Down
24 changes: 24 additions & 0 deletions lib/Html2Docx/document_objects/heading.rb
@@ -0,0 +1,24 @@
module Html2Docx
module DocumentObjects
class Heading
def initialize(document)
@document = document
@heading = nil
end

def add_heading(heading_object)
heading_object['class'] = "Heading#{heading_object.name.scan(/[0-9]/).first}"
heading_object.name = 'p'

paragraph = Paragraph.new(@document, nil)
paragraph.add_paragraph(heading_object)

@heading = paragraph.render
end

def render
@heading
end
end
end
end
57 changes: 36 additions & 21 deletions lib/Html2Docx/document_objects/paragraph.rb
Expand Up @@ -14,34 +14,42 @@ def add_paragraph(paragraph_object)
def create_paragraph(paragraph_object)
@paragraph = Nokogiri::XML::Node.new('w:p', @document)

paragraph_style = paragraph_object.attr('style')
add_paragraph_style paragraph_style if paragraph_style

add_paragraph_style paragraph_object
add_paragraph_child paragraph_object.children
end

def add_paragraph_style(style_attribute)
paragraph_style = Nokogiri::XML::Node.new('w:pPr', @document)
def add_paragraph_style(paragraph_object)
paragraph_style = Nokogiri::XML::Node.new('w:pPr', @document)
paragraph_styles = []

styles = style_attribute.split(';')

styles.each do |style|
style = style.strip
attribute, value = style.scan(/(.+):\s?(.+);?/).flatten

case attribute
when 'text-indent'
paragraph_styles.push add_paragraph_indent(value)
when 'text-align'
paragraph_styles.push add_paragraph_alignment(value)
when 'background-color'
paragraph_styles.push add_paragraph_background_color(value)
when 'line-height'
paragraph_styles.push add_line_height(value)
style_attribute = paragraph_object.attr('style')
style_attributes = style_attribute.split(';') if style_attribute

paragraph_class = paragraph_object.attr('class')
paragraph_class = paragraph_class.split(' ')&.first if paragraph_class

if style_attributes
style_attributes.each do |style|
style = style.strip
attribute, value = style.scan(/(.+):\s?(.+);?/).flatten

case attribute
when 'text-indent'
paragraph_styles.push add_paragraph_indent(value)
when 'text-align'
paragraph_styles.push add_paragraph_alignment(value)
when 'background-color'
paragraph_styles.push add_paragraph_background_color(value)
when 'line-height'
paragraph_styles.push add_paragraph_line_height(value)
end
end
end

unless paragraph_class.nil?
paragraph_styles.push add_paragraph_class(paragraph_class)
end

paragraph_styles.each do |style|
paragraph_style.add_child(style)
end
Expand All @@ -56,6 +64,13 @@ def add_paragraph_indent(value)
indent_tag
end

def add_paragraph_class(value)
class_tag = Nokogiri::XML::Node.new('w:pStyle', @document)
class_tag['w:val'] = value

class_tag
end

def add_paragraph_alignment(value)
align_tag = Nokogiri::XML::Node.new('w:jc', @document)
value = value.downcase
Expand All @@ -74,7 +89,7 @@ def add_paragraph_background_color(value)
background_tag
end

def add_line_height(value)
def add_paragraph_line_height(value)
line_height_tag = Nokogiri::XML::Node.new('w:spacing', @document)
line_height_tag['w:line'] = Helpers::DocumentHelper.line_height(value)

Expand Down
1 change: 0 additions & 1 deletion samples/EmptyPage.docx_FILES/[Content_Types].xml

This file was deleted.

1 change: 0 additions & 1 deletion samples/EmptyPage.docx_FILES/_rels/.rels

This file was deleted.

1 change: 0 additions & 1 deletion samples/EmptyPage.docx_FILES/docProps/app.xml

This file was deleted.

1 change: 0 additions & 1 deletion samples/EmptyPage.docx_FILES/docProps/core.xml

This file was deleted.

1 change: 0 additions & 1 deletion samples/EmptyPage.docx_FILES/word/_rels/document2.xml.rels

This file was deleted.

1 change: 0 additions & 1 deletion samples/EmptyPage.docx_FILES/word/document2.xml

This file was deleted.

1 change: 0 additions & 1 deletion samples/EmptyPage.docx_FILES/word/fontTable.xml

This file was deleted.

1 change: 0 additions & 1 deletion samples/EmptyPage.docx_FILES/word/settings.xml

This file was deleted.

1 change: 0 additions & 1 deletion samples/EmptyPage.docx_FILES/word/styles.xml

This file was deleted.

0 comments on commit 60811c2

Please sign in to comment.