Commit 1858a237 by Edgar HIPP

Use own docUtils instead of xmlTemplater docUtils

parent 3eb831fb
DOMParser = require('xmldom').DOMParser
XMLSerializer= require('xmldom').XMLSerializer
DocUtils= require('docxtemplater').DocUtils
DocUtils = {}
DocUtils.xml2Str = (xmlNode) ->
a= new XMLSerializer()
......@@ -13,4 +13,34 @@ DocUtils.Str2xml= (str,errorHandler) ->
DocUtils.maxArray = (a) -> Math.max.apply(null, a)
DocUtils.decodeUtf8= (s) ->
try
if s==undefined then return undefined
return decodeURIComponent(escape(DocUtils.convertSpaces(s))) #replace Ascii 160 space by the normal space, Ascii 32
catch e
console.error s
console.error 'could not decode'
throw new Error('end')
DocUtils.encodeUtf8 = (s)->
unescape(encodeURIComponent(s))
DocUtils.convertSpaces= (s) ->
s.replace(new RegExp(String.fromCharCode(160),"g")," ")
DocUtils.pregMatchAll= (regex, content) ->
###regex is a string, content is the content. It returns an array of all matches with their offset, for example:
regex=la
content=lolalolilala
returns: [{0:'la',offset:2},{0:'la',offset:8},{0:'la',offset:10}]
###
regex= (new RegExp(regex,'g')) unless (typeof regex=='object')
matchArray= []
replacer = (match,pn ..., offset, string)->
pn.unshift match #add match so that pn[0] = whole match, pn[1]= first parenthesis,...
pn.offset= offset
matchArray.push pn
content.replace regex,replacer
matchArray
module.exports=DocUtils
......@@ -24,7 +24,7 @@ module.exports = class ImgManager
loadImageRels: () ->
file=@zip.files["word/_rels/#{@endFileName}.xml.rels"] || @zip.files["word/_rels/document.xml.rels"]
if file==undefined then return
content= DocUtils.decode_utf8 file.asText()
content= DocUtils.decodeUtf8 file.asText()
@xmlDoc= DocUtils.Str2xml content
RidArray = ((parseInt tag.getAttribute("Id").substr(3)) for tag in @xmlDoc.getElementsByTagName('Relationship')) #Get all Rids
@maxRid=DocUtils.maxArray(RidArray)
......@@ -32,7 +32,6 @@ module.exports = class ImgManager
this
addExtensionRels: (contentType,extension) -> #Add an extension type in the [Content_Types.xml], is used if for example you want word to be able to read png files (for every extension you add you need a contentType)
#content = DocUtils.decode_utf8 @zip.files["[Content_Types].xml"].asText()
content = @zip.files["[Content_Types].xml"].asText()
xmlDoc= DocUtils.Str2xml content
addTag= true
......@@ -46,7 +45,7 @@ module.exports = class ImgManager
newTag.setAttribute('ContentType',contentType)
newTag.setAttribute('Extension',extension)
types.appendChild newTag
@setImage "[Content_Types].xml",DocUtils.encode_utf8 DocUtils.xml2Str xmlDoc
@setImage "[Content_Types].xml",DocUtils.encodeUtf8 DocUtils.xml2Str xmlDoc
addImageRels: (imageName,imageData,i=0) -> #Adding an image and returns it's Rid
realImageName=if i==0 then imageName else imageName+"(#{i})"
if @zip.files["word/media/#{realImageName}"]?
......@@ -71,7 +70,7 @@ module.exports = class ImgManager
newTag.setAttribute('Type','http://schemas.openxmlformats.org/officeDocument/2006/relationships/image')
newTag.setAttribute('Target',"media/#{realImageName}")
relationships.appendChild newTag
@setImage("word/_rels/#{@endFileName}.xml.rels",DocUtils.encode_utf8 DocUtils.xml2Str @xmlDoc)
@setImage("word/_rels/#{@endFileName}.xml.rels",DocUtils.encodeUtf8 DocUtils.xml2Str @xmlDoc)
@maxRid
getImageName:(id=0)->
nameCandidate="Copie_"+id+".png"
......
......@@ -9,7 +9,7 @@ module.exports= class ImgReplacer
@xmlTemplater.numQrCode=0
this
findImages:() ->
@imgMatches= DocUtils.preg_match_all ///
@imgMatches= DocUtils.pregMatchAll ///
<w:drawing[^>]*>
.*?<a:blip.r:embed.*?
</w:drawing>
......@@ -28,8 +28,6 @@ module.exports= class ImgReplacer
@popQrQueue(@imgManager.fileName+'-'+docxqrCode.num,false)
replaceImage:(match,u)->
num=parseInt(Math.random()*10000)
# num=@xmlTemplater.DocxGen.qrCodeNumCallBack
# @xmlTemplater.DocxGen.qrCodeNumCallBack++
try
xmlImg= DocUtils.Str2xml '<?xml version="1.0" ?><w:document mc:Ignorable="w14 wp14" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">'+match[0]+'</w:document>',(_i,type)->if _i=='fatalError' then throw "fatalError"
catch e
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment