Skip to content

Commit

Permalink
Allow paths in file names; added save method to document instances; f…
Browse files Browse the repository at this point in the history
…ixed issue with empty paragraphs following tables when they actually needed to be inserted in table cells sometimes.
  • Loading branch information
jdugan committed Aug 31, 2018
1 parent 3189588 commit 7abe778
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 29 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
#### v1.4.0

* Enhancements
* Changed `file_name` method to accept absolute and relative paths. (@jdugan)
* Added instance save method so documents can be created via commands instead of blocks, if desired. (@jdugan)
* Removed extra paragraph tag inserted after tables; added logic to ensure final tag of table cells is always a paragraph. (@jdugan)


#### v1.3.0

* Enhancements
* Added proper text highlighting. Colors are limited so we left :bgcolor option. (@rmarone)
* Added bookmarks and internal links. (@rmarone)


#### v1.2.0

* Enhancements
Expand Down
28 changes: 16 additions & 12 deletions lib/caracal/core/file_name.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
module Caracal
module Core
# This module encapsulates all the functionality related to setting the

# This module encapsulates all the functionality related to setting the
# document's name.
#
module FileName
def self.included(base)
base.class_eval do

#-------------------------------------------------------------
# Configuration
#-------------------------------------------------------------

# constants
const_set(:DEFAULT_FILE_NAME, 'caracal.docx')

# accessors
attr_reader :name


attr_reader :path


#-------------------------------------------------------------
# Public Methods
#-------------------------------------------------------------

# This method sets the name of the output file. Defaults
# to the name of the library.
#
def file_name(value=nil)
v = value.to_s.strip
@name = (v == '') ? self.class::DEFAULT_FILE_NAME : v
a = v.split('/')

@name = (v == '') ? self.class::DEFAULT_FILE_NAME : a.last
@path = (a.size > 1) ? v : "./#{ v }"
end

end
end
end

end
end
end
6 changes: 6 additions & 0 deletions lib/caracal/core/models/table_cell_model.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'caracal/core/models/base_model'
require 'caracal/core/models/margin_model'
require 'caracal/core/models/paragraph_model'


module Caracal
Expand Down Expand Up @@ -39,6 +40,11 @@ def initialize(options={}, &block)
end

super options, &block

p_klass = Caracal::Core::Models::ParagraphModel # the final tag in a table cell
unless contents.last.is_a? p_klass # *must* be a paragraph for OOXML
contents << p_klass.new(content: '') # to not throw an error.
end
end


Expand Down
39 changes: 24 additions & 15 deletions lib/caracal/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
module Caracal
class Document

#-------------------------------------------------------------
#------------------------------------------------------
# Configuration
#-------------------------------------------------------------
#------------------------------------------------------

# mixins (order is important)
include Caracal::Core::CustomProperties
Expand All @@ -64,11 +64,11 @@ class Document
include Caracal::Core::Text


#-------------------------------------------------------------
#------------------------------------------------------
# Public Class Methods
#-------------------------------------------------------------
#------------------------------------------------------

#============ OUTPUT ====================================
#============ OUTPUT ==================================

# This method renders a new Word document and returns it as a
# a string.
Expand All @@ -86,16 +86,17 @@ def self.render(f_name = nil, &block)
#
def self.save(f_name = nil, &block)
docx = new(f_name, &block)
buffer = docx.render

File.open("./#{ docx.name }", 'wb') { |f| f.write(buffer.string) }
docx.save
# buffer = docx.render
#
# File.open(docx.path, 'wb') { |f| f.write(buffer.string) }
end



#-------------------------------------------------------------
#------------------------------------------------------
# Public Instance Methods
#-------------------------------------------------------------
#------------------------------------------------------

# This method instantiates a new word document.
#
Expand All @@ -119,7 +120,7 @@ def initialize(name = nil, &block)
end


#============ GETTERS ===================================
#============ GETTERS =================================

# This method returns an array of models which constitute the
# set of instructions for producing the document content.
Expand All @@ -129,7 +130,7 @@ def contents
end


#============ RENDERING =================================
#============ RENDERING ===============================

# This method renders the word document instance into
# a string buffer. Order is important!
Expand All @@ -153,13 +154,21 @@ def render
end


#============ SAVING ==================================

def save
buffer = render

File.open(path, 'wb') { |f| f.write(buffer.string) }
end


#-------------------------------------------------------------
#------------------------------------------------------
# Private Instance Methods
#-------------------------------------------------------------
#------------------------------------------------------
private

#============ RENDERERS =====================================
#============ RENDERERS ===============================

def render_app(zip)
content = ::Caracal::Renderers::AppRenderer.render(self)
Expand Down
2 changes: 1 addition & 1 deletion lib/caracal/renderers/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ def render_table(xml, model)

# don't know why this is needed, but it prevents a
# rendering error.
render_paragraph(xml, Caracal::Core::Models::ParagraphModel.new)
# render_paragraph(xml, Caracal::Core::Models::ParagraphModel.new)
end


Expand Down
2 changes: 1 addition & 1 deletion lib/caracal/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Caracal
VERSION = '1.3.0'
VERSION = '1.4.0'
end

0 comments on commit 7abe778

Please sign in to comment.