Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammetDilmac committed Jun 22, 2017
2 parents 73453cc + 7f53481 commit 5f4b7a9
Show file tree
Hide file tree
Showing 57 changed files with 763 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
@@ -0,0 +1,13 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
/.idea/

# rspec failure tracking
.rspec_status
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--format documentation
--color
5 changes: 5 additions & 0 deletions .travis.yml
@@ -0,0 +1,5 @@
sudo: false
language: ruby
rvm:
- 2.4.0
before_install: gem install bundler -v 1.15.1
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in Html2Docx.gemspec
gemspec
31 changes: 31 additions & 0 deletions Html2Docx.gemspec
@@ -0,0 +1,31 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'Html2Docx/version'

Gem::Specification.new do |spec|
spec.name = 'Html2Docx'
spec.version = Html2Docx::VERSION
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.homepage = 'https://www.github.com/MuhammetDilmac/Html2Docx'
spec.license = 'MIT'

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_development_dependency 'bundler', '~> 1.15'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'nokogiri', '~> 1.6', '>= 1.6.8'
spec.add_development_dependency 'rubyzip', '~> 1.2.0'
end
21 changes: 21 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 MuhammetDilmac

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
35 changes: 35 additions & 0 deletions README.md
@@ -0,0 +1,35 @@
# Html2Docx

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/Html2Docx`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'Html2Docx'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install Html2Docx

## Usage

Html2Docx has very easy usage.

This time only supporting paragraph output. [Wiki - Paragraph Usage](https://github.com/MuhammetDilmac/Html2Docx/wiki/Paragraph-Usage)

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/MuhammetDilmac/Html2Docx. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.

## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
6 changes: 6 additions & 0 deletions Rakefile
@@ -0,0 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
14 changes: 14 additions & 0 deletions bin/console
@@ -0,0 +1,14 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "Html2Docx"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions bin/setup
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
50 changes: 50 additions & 0 deletions lib/Html2Docx.rb
@@ -0,0 +1,50 @@
require 'fileutils'
require 'nokogiri'
require 'zip'

require 'Html2Docx/helpers/document_helper'
require 'Html2Docx/helpers/nokogiri_helper'
require 'Html2Docx/helpers/zip_file_generator'

require 'Html2Docx/version'
require 'Html2Docx/initialization'
require 'Html2Docx/content_types'
require 'Html2Docx/relation'
require 'Html2Docx/document'
require 'Html2Docx/document_objects/paragraph'

module Html2Docx
ROOT_PATH = File.expand_path(File.join(File.dirname(__FILE__), '../'))

def self.clear_temp(tmp)
FileUtils.rm_r tmp
end

def self.create_docx(output, input)
zf = ZipFileGenerator.new(input, output)
zf.write

self.clear_temp(input)
end

def self.render(options = {})
initialization = Initialization.new(options)
options[:temp] = initialization.get_temp_directory

content_types = ContentTypes.new(options)

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

document = Document.new(options)

# Render
document.render
content_types.render
relation.render

# Create Docx File
self.create_docx(options.fetch(:output), options.fetch(:temp))
end
end
51 changes: 51 additions & 0 deletions lib/Html2Docx/content_types.rb
@@ -0,0 +1,51 @@
module Html2Docx
class ContentTypes
def initialize(options = {})
@content_type_file = File.join(options.fetch(:temp), '[Content_Types].xml')
@content_type = File.open(@content_type_file) {|f| Nokogiri::XML(f)}
@parts = {default: [], override: []}
initial_parts
end

def initial_parts
@content_type.root.children.each do |child|
if child.name == 'Default'
@parts[:default].push({extension: child.attr('Extension'), content_type: child.attr('ContentType')})
elsif child.name == 'Override'
@parts[:override].push({part_name: child.attr('PartName'), content_type: child.attr('ContentType')})
end

child.remove
end
end

def add_parts(object)
if object.fetch(:type) == 'Default'
@parts[:default].push({extension: object.fetch(:extension), content_type: object.fetch(:content_type)})
elsif object.fetch(:type) == 'Override'
@parts[:override].push({part_name: object.fetch(:part_name), content_type: object.fetch(:content_type)})
end
end

def render
@parts.fetch(:default).each { |child| add_default child}
@parts.fetch(:override).each { |child| add_override child}

File.open(@content_type_file, 'w') {|f| f.write Helpers::NokogiriHelper.to_xml(@content_type)}
end

def add_default(child)
node = Nokogiri::XML::Node.new('Default', @content_type)
node['Extension'] = child.fetch(:extension, '')
node['ContentType'] = child.fetch(:content_type, '')
@content_type.root << node
end

def add_override(child)
node = Nokogiri::XML::Node.new('Override', @content_type)
node['PartName'] = child.fetch(:part_name, '')
node['ContentType'] = child.fetch(:content_type, '')
@content_type.root << node
end
end
end
95 changes: 95 additions & 0 deletions lib/Html2Docx/document.rb
@@ -0,0 +1,95 @@
module Html2Docx
class Document
def initialize(options = {})
@document_file = File.join(options.fetch(:temp), 'word', 'document2.xml')
@document = File.open(@document_file) { |f| Nokogiri::XML(f) }
@body = @document.at_xpath('//w:body')
@contents = []

initial_body
add_html(options[:html])
end

def initial_body
@body.children.each do |child|
child.remove
end
end

def add_html(html)
html = Nokogiri::HTML(html.gsub!(/\sl\s+|\n/, ' '))

elements = html.css('body')

elements.children.each do |element|
case element.name
when 'p'
# Add paragraph
paragraph = DocumentObjects::Paragraph.new(@document, nil)
paragraph.add_paragraph(element)
@contents.push paragraph.render
when 'table'
# Add table
@contents.push ''
end
end
end

def render
@contents.each do |content|
@body << content
end

@body << sectPr

@document.root.add_child(@body)

File.open(@document_file, 'w') { |f| f.write(Helpers::NokogiriHelper.to_xml(@document)) }
end

def sectPr
root = Nokogiri::XML::Node.new('w:sectPr', @document)
root.add_child(pgSz)
root.add_child(pgMar)
root.add_child(cols)
root.add_child(docGrid)

root
end

def pgSz
pgSz = Nokogiri::XML::Node.new('w:pgSz', @document)
pgSz['w:w'] = '12240'
pgSz['w:h'] = '15840'

pgSz
end

def pgMar
pgMar = Nokogiri::XML::Node.new('w:pgMar', @document)
pgMar['w:top'] = '1440'
pgMar['w:right'] = '1440'
pgMar['w:bottom'] = '1440'
pgMar['w:left'] = '1440'
pgMar['w:header'] = '720'
pgMar['w:footer'] = '720'
pgMar['w:gutter'] = '0'

pgMar
end

def cols
cols = Nokogiri::XML::Node.new('w:cols', @document)
cols['w:space'] = '720'

cols
end

def docGrid
docGrid = Nokogiri::XML::Node.new('w:docGrid', @document)
docGrid['w:linePitch'] = '360'

docGrid
end
end
end

0 comments on commit 5f4b7a9

Please sign in to comment.