Commit ebea6f31 by Edgar HIPP

Add convert scripts

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