Commit ebea6f31 by Edgar HIPP

Add convert scripts

parent 38a1132e
{
"presets": ["es2015"]
}
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
test/ test/
js/ js/
node_modules node_modules
es6
...@@ -6,9 +6,3 @@ node_js: ...@@ -6,9 +6,3 @@ node_js:
- "0.10" - "0.10"
- "4" - "4"
- "iojs" - "iojs"
before_script:
- "npm install -g npm"
- "npm install -g gulp"
- "npm install -g mocha"
- "gulp allCoffee"
...@@ -34,11 +34,13 @@ DocUtils.pregMatchAll= (regex, content) -> ...@@ -34,11 +34,13 @@ DocUtils.pregMatchAll= (regex, content) ->
content=lolalolilala content=lolalolilala
returns: [{0:'la',offset:2},{0:'la',offset:8},{0:'la',offset:10}] returns: [{0:'la',offset:2},{0:'la',offset:8},{0:'la',offset:10}]
### ###
if typeof regex!='object' if (typeof regex!='object')
regex = new RegExp(regex,'g') regex= (new RegExp(regex,'g'))
matchArray= [] matchArray= []
replacer = (match,pn..., offset, string)-> replacer = (pn...)->
pn.unshift match #add match so that pn[0] = whole match, pn[1]= first parenthesis,... string = pn.pop()
offset = pn.pop()
#add match so that pn[0] = whole match, pn[1]= first parenthesis,...
pn.offset= offset pn.offset= offset
matchArray.push pn matchArray.push pn
content.replace regex,replacer content.replace regex,replacer
......
DocUtils=require('./docUtils') DocUtils=require('./docUtils')
imageExtensions=['gif','jpeg','jpg','emf','png']
module.exports = class ImgManager module.exports = class ImgManager
imageExtensions=['gif','jpeg','jpg','emf','png']
constructor:(@zip,@fileName)-> constructor:(@zip,@fileName)->
@endFileName=@fileName.replace(/^.*?([a-z0-9]+)\.xml$/,"$1") @endFileName=@fileName.replace(/^.*?([a-z0-9]+)\.xml$/,"$1")
getImageList: () -> getImageList: () ->
regex= /// regex= ///
[^.]+ #name [^.]+
\. #dot \.
([^.]+) #extension ([^.]+)
/// ///
imageList= [] imageList= []
for index of @zip.files for index of @zip.files
...@@ -26,11 +27,13 @@ module.exports = class ImgManager ...@@ -26,11 +27,13 @@ module.exports = class ImgManager
if file==undefined then return if file==undefined then return
content= DocUtils.decodeUtf8 file.asText() content= DocUtils.decodeUtf8 file.asText()
@xmlDoc= DocUtils.Str2xml content @xmlDoc= DocUtils.Str2xml content
RidArray = ((parseInt tag.getAttribute("Id").substr(3)) for tag in @xmlDoc.getElementsByTagName('Relationship')) #Get all Rids # Get all Rids
RidArray = []
for tag in @xmlDoc.getElementsByTagName('Relationship')
RidArray.push(parseInt tag.getAttribute("Id").substr(3))
@maxRid=DocUtils.maxArray(RidArray) @maxRid=DocUtils.maxArray(RidArray)
@imageRels=[] @imageRels=[]
this 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) 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 = @zip.files["[Content_Types].xml"].asText() content = @zip.files["[Content_Types].xml"].asText()
xmlDoc= DocUtils.Str2xml content xmlDoc= DocUtils.Str2xml content
......
...@@ -9,11 +9,7 @@ module.exports= class ImgReplacer ...@@ -9,11 +9,7 @@ module.exports= class ImgReplacer
@xmlTemplater.numQrCode=0 @xmlTemplater.numQrCode=0
this this
findImages:() -> findImages:() ->
@imgMatches= DocUtils.pregMatchAll /// @imgMatches= DocUtils.pregMatchAll /<w:drawing[^>]*>.*?<a:blip.r:embed.*?<\/w:drawing>/g, @xmlTemplater.content
<w:drawing[^>]*>
.*?<a:blip.r:embed.*?
</w:drawing>
///g, @xmlTemplater.content
this this
replaceImages: ()-> replaceImages: ()->
@qr=[] @qr=[]
...@@ -29,7 +25,29 @@ module.exports= class ImgReplacer ...@@ -29,7 +25,29 @@ module.exports= class ImgReplacer
replaceImage:(match,u)-> replaceImage:(match,u)->
num=parseInt(Math.random()*10000) num=parseInt(Math.random()*10000)
try 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" baseDocument = """<?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>
"""
f=(_i,type)->
if _i=='fatalError'
throw "fatalError"
xmlImg= DocUtils.Str2xml baseDocument,f
catch e catch e
return return
tagrId= xmlImg.getElementsByTagName("a:blip")[0] tagrId= xmlImg.getElementsByTagName("a:blip")[0]
......
...@@ -32,8 +32,8 @@ class ImageModule ...@@ -32,8 +32,8 @@ class ImageModule
xmlTemplater=@manager.getInstance('xmlTemplater') xmlTemplater=@manager.getInstance('xmlTemplater')
templaterState=@manager.getInstance('templaterState') templaterState=@manager.getInstance('templaterState')
subContent=new SubContent(xmlTemplater.content) subContent=new SubContent(xmlTemplater.content)
.getInnerTag(templaterState) subContent=subContent.getInnerTag(templaterState)
.getOuterXml(outsideElement) subContent=subContent.getOuterXml(outsideElement)
xmlTemplater.replaceXml(subContent,text) xmlTemplater.replaceXml(subContent,text)
convertPixelsToEmus:(pixel)-> convertPixelsToEmus:(pixel)->
Math.round(pixel * 9525) Math.round(pixel * 9525)
...@@ -91,9 +91,7 @@ class ImageModule ...@@ -91,9 +91,7 @@ class ImageModule
num=parseInt(Math.random()*10000) num=parseInt(Math.random()*10000)
imR.pushQrQueue("rendered-" + num) imR.pushQrQueue("rendered-" + num)
try try
imR imR.findImages().replaceImages()
.findImages()
.replaceImages()
catch e catch e
@on('error',e) @on('error',e)
f=()=>imR.popQrQueue("rendered-" + num) f=()=>imR.popQrQueue("rendered-" + num)
......
var gulp = require('gulp');
var watch= require('gulp-watch');
var coffee= require('gulp-coffee');
var uglify= require('gulp-uglify');
var config={uglify:false}
var paths = {
coffee: ['coffee/subContent.coffee','coffee/docxQrCode.coffee','coffee/xmlUtil.coffee','coffee/docUtils.coffee','coffee/imgManager.coffee','coffee/imgReplacer.coffee','coffee/index.coffee'],
coffeeTest: ['coffee/test.coffee'],
testDirectory:__dirname+'/test',
js:'js/'
};
gulp.task('allCoffee', function () {
gulp.src(paths.coffee)
.pipe(coffee({bare:true}))
.pipe(gulp.dest(paths.js))
a=gulp.src(paths.coffeeTest)
.pipe(coffee({map:true}))
if(config.uglify)
a=a.pipe(uglify())
a=a
.pipe(gulp.dest(paths.testDirectory));
});
gulp.task('watch', function () {
gulp.src(paths.coffee)
.pipe(watch(function(files) {
var f=files.pipe(coffee({bare:true}))
.pipe(gulp.dest(paths.js))
return f;
}));
gulp.watch(paths.coffeeTest,['coffeeTest']);
});
gulp.task('coffeeTest', function() {
a=gulp.src(paths.coffeeTest)
.pipe(coffee({map:true}))
if(config.uglify)
a=a.pipe(uglify())
a=a
.pipe(gulp.dest(paths.testDirectory));
return a;
});
gulp.task('default',['coffeeTest','watch']);
...@@ -4,15 +4,17 @@ ...@@ -4,15 +4,17 @@
"description": "Image Module for docxtemplater v1.0", "description": "Image Module for docxtemplater v1.0",
"main": "js/index.js", "main": "js/index.js",
"scripts": { "scripts": {
"test": "mocha" "convertto:es6": "rm es6 -rf; mkdir -p es6; for i in coffee/*.coffee; do echo \"$i\"; decaffeinate \"$i\"; mv \"${i%.coffee}.js\" \"${i%.coffee}.es6\"; done; for i in coffee/*.es6; do mv \"$i\" \"es6${i#coffee}\"; done ",
"convertto:es5": "rm js -rf ; mkdir -p test/spec; mkdir -p js; babel es6 --out-dir js; mv js/test.js test/test.js",
"test:compiled": "mocha",
"test": "npm run convertto:es6 && npm run convertto:es5 && npm run test:compiled"
}, },
"devDependencies": { "devDependencies": {
"chai": "^1.10.0", "babel-preset-es2015": "^6.3.13",
"gulp": "~3.8.0", "chai": "^3.4.1",
"gulp-coffee": "~2.0.1", "docxtemplater": "^2.0.0",
"gulp-uglify": "~0.3.0", "eslint": "^1.10.3",
"gulp-browserify": "^0.5.0", "mocha": "^2.4.5"
"gulp-watch": "~0.6.5"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
...@@ -21,10 +23,11 @@ ...@@ -21,10 +23,11 @@
"author": "Edgar Hipp", "author": "Edgar Hipp",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"docxtemplater": "^1.0.8", "decaffeinate": "^1.44.12",
"docxtemplater": "^2.0.0",
"jszip": "^2.4.0", "jszip": "^2.4.0",
"png-js": "^0.1.1", "png-js": "^0.1.1",
"qrcode-reader": "0.0.5", "qrcode-reader": "0.0.7",
"xmldom": "^0.1.19" "xmldom": "0.1.19"
} }
} }
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