Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/htmltoword/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,26 @@ def generate
def replace_files(html, extras = false)
html = '<body></body>' if html.nil? || html.empty?
source = Nokogiri::HTML(html.gsub(/>\s+</, '><'))

#rowspan workaround
source.xpath('//td[@rowspan > 1]').each do |e|
cols = 0
prev = e
cols += prev.attributes['colspan'] ? prev.attributes['colspan'].value.to_i : 1 while(prev = prev.previous)
parent = e.parent
(e.attributes['rowspan'].value.to_i - 1).times do
n = parent.next
n.children.inject(0) do |sum, ch|
if sum == cols
ch.add_previous_sibling "<td vmerge colspan=\"#{e.attributes['colspan'] ? e.attributes['colspan'].value : 1}\"></td>"
break
end
sum += ch.attributes['colspan'] ? ch.attributes['colspan'].value.to_i : 1
sum
end
end
end

transform_and_replace(source, xslt_path('numbering'), Document.numbering_xml_file)
transform_and_replace(source, xslt_path('relations'), Document.relations_xml_file)
transform_doc_xml(source, extras)
Expand Down
6 changes: 6 additions & 0 deletions lib/htmltoword/xslt/tables.xslt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@
<xsl:if test="@colspan &gt; 1">
<w:gridSpan w:val="{@colspan}"/>
</xsl:if>
<xsl:if test="@rowspan &gt; 1">
<w:vMerge w:val="restart"/>
</xsl:if>
<xsl:if test="@vmerge">
<w:vMerge/>
</xsl:if>
</w:tcPr>
</xsl:template>

Expand Down