Skip to content

Commit 7a6b2c4

Browse files
committed
Merge branch 'master' into 1.x
Conflicts: coffee/docxgenTest.coffee coffee/xmlTemplater.coffee
2 parents 1e73d09 + 394b955 commit 7a6b2c4

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

coffee/docxgenTest.coffee

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
docX={}
2-
docXData={}
32

43
expressions= require('angular-expressions')
54
angularParser= (tag) ->
@@ -127,10 +126,10 @@ describe "DocxGenTemplatingForLoop", () ->
127126
"prenom":"Edgar"
128127
"telephone":"0652455478"
129128
"description":"New Website"
130-
"offre":[{"titre":"titre1","prix":"1250"},{"titre":"titre2","prix":"2000"},{"titre":"titre3","prix":"1400"}]
129+
"offre":[{"titre":"titre1","prix":"1250"},{"titre":"titre2","prix":"2000"},{"titre":"titre3","prix":"1400", "nom": "Offre"}]
131130
docX['tagLoopExample.docx'].setTags Tags
132131
docX['tagLoopExample.docx'].applyTags()
133-
expect(docX['tagLoopExample.docx'].getFullText()).toEqual('Votre proposition commercialePrix: 1250Titre titre1Prix: 2000Titre titre2Prix: 1400Titre titre3HippEdgar')
132+
expect(docX['tagLoopExample.docx'].getFullText()).toEqual('Votre proposition commercialeHippPrix: 1250Titre titre1HippPrix: 2000Titre titre2OffrePrix: 1400Titre titre3HippEdgar')
134133
it "should work with loops inside loops", () ->
135134
Tags = {"products":[{"title":"Microsoft","name":"DOS","reference":"Win7","avantages":[{"title":"Everyone uses it","proof":[{"reason":"it is quite cheap"},{"reason":"it is quit simple"},{"reason":"it works on a lot of different Hardware"}]}]},{"title":"Linux","name":"Ubuntu","reference":"Ubuntu10","avantages":[{"title":"It's very powerful","proof":[{"reason":"the terminal is your friend"},{"reason":"Hello world"},{"reason":"it's free"}]}]},{"title":"Apple","name":"Mac","reference":"OSX","avantages":[{"title":"It's very easy","proof":[{"reason":"you can do a lot just with the mouse"},{"reason":"It's nicely designed"}]}]},]}
136135
docX['tagProduitLoop.docx'].setTags Tags
@@ -262,9 +261,7 @@ describe "getTags", () ->
262261
it "should work with loop document", () ->
263262
docX['tagLoopExample.docx']=new DocxGen docX['tagLoopExample.docx'].loadedContent,{},{intelligentTagging:off}
264263
tempVars= docX['tagLoopExample.docx'].getTags()
265-
expect(tempVars).toEqual([ { fileName : 'word/document.xml', vars : { offre : { prix : true, titre : true }, nom : true, prenom : true } }, { fileName : 'word/footer1.xml', vars : { nom : true, prenom : true, telephone : true } }, { fileName : 'word/header1.xml', vars : { nom : true, prenom : true } } ])
266-
267-
264+
expect(tempVars).toEqual([ { fileName : 'word/document.xml', vars : { offre : { nom: true, prix : true, titre : true }, nom : true, prenom : true } }, { fileName : 'word/footer1.xml', vars : { nom : true, prenom : true, telephone : true } }, { fileName : 'word/header1.xml', vars : { nom : true, prenom : true } } ])
268265

269266
describe "xmlTemplater", ()->
270267
it "should work with simpleContent", ()->
@@ -594,6 +591,17 @@ describe 'SubContent', () ->
594591
xmlt=new DocXTemplater(content,{Tags:{name:"Henry"}}).applyTags()
595592
expect(xmlt.content).toContain('Henry</w:t><w:t')
596593

594+
describe 'getting parents context',()->
595+
it 'should work with simple loops',()->
596+
content= """{#loop}{name}{/loop}"""
597+
xmlt=new DocXTemplater(content,{Tags:{loop:[1],name:"Henry"}}).applyTags()
598+
expect(xmlt.content).toBe("Henry")
599+
600+
it 'should work with double loops',()->
601+
content= """{#loop_first}{#loop_second}{name_inner} {name_outer}{/loop_second}{/loop_first}"""
602+
xmlt=new DocXTemplater(content,{Tags:{loop_first:[1],loop_second:[{name_inner:"John"}],name_outer:"Henry"}}).applyTags()
603+
expect(xmlt.content).toBe("John Henry")
604+
597605
describe 'error messages', ()->
598606
it 'should work with unclosed', ()->
599607
content= """<w:t>{tag {age}</w:t>"""

coffee/scopeManager.coffee

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,31 @@
22
DocUtils=require('./docUtils')
33

44
module.exports=class ScopeManager
5-
constructor:(@tags,@scopePath,@usedTags,@currentScope,@parser)->
5+
constructor:(@tags,@scopePath,@usedTags,@scopeList,@parser)->
66
loopOver:(tag,callback,inverted=false)->
77
value = @getValue(tag)
88
type = typeof value
99
if inverted
10-
return callback(@currentScope) unless value
10+
return callback(@scopeList[@num]) unless value
1111
return if type == 'string'
1212
if type == 'object' && value.length < 1
13-
callback(@currentScope)
13+
callback(@scopeList[@num])
1414
return
1515
return unless value?
1616
if type == 'object'
1717
for scope,i in value
1818
callback(scope)
1919
if value == true
20-
callback(@currentScope)
21-
getValue:(tag)->
20+
callback(@scopeList[@num])
21+
getValue:(tag,@num=@scopeList.length-1)->
22+
scope=@scopeList[@num]
2223
parser=@parser(DocUtils.wordToUtf8(tag))
23-
result=parser.get(@currentScope)
24+
result=parser.get(scope)
25+
if result==undefined and @num>0 then return @getValue(tag,@num-1)
26+
result
2427
getValueFromScope: (tag) ->
25-
result=@getValue(tag)
28+
# search in the scopes (in reverse order) and keep the first defined value
29+
result = @getValue(tag)
2630
if result?
2731
if typeof result=='string'
2832
@useTag(tag)

coffee/xmlTemplater.coffee

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports=class XmlTemplater #abstract class !!
1111
@currentClass=XmlTemplater #This is used because tags are recursive, so the class needs to be able to instanciate an object of the same class. I created a variable so you don't have to Override all functions relative to recursivity
1212
@fromJson(options)
1313
@templaterState= new TemplaterState
14-
@currentScope=@Tags
1514
load: (@content) ->
1615
xmlMatcher=new XmlMatcher(@content).parse(@tagXml)
1716
@templaterState.matches = xmlMatcher.matches
@@ -21,16 +20,18 @@ module.exports=class XmlTemplater #abstract class !!
2120
@DocxGen= if options.DocxGen? then options.DocxGen else null
2221
@intelligentTagging=if options.intelligentTagging? then options.intelligentTagging else off
2322
@scopePath=if options.scopePath? then options.scopePath else []
23+
@scopeList= if options.scopeList? then options.scopeList else [@Tags]
2424
@usedTags=if options.usedTags? then options.usedTags else {}
2525
@imageId=if options.imageId? then options.imageId else 0
2626
@parser= if options.parser? then options.parser else DocUtils.defaultParser
2727
@fileName=options.fileName
28-
@scopeManager=new ScopeManager(@Tags,@scopePath,@usedTags,@Tags,@parser)
28+
@scopeManager=new ScopeManager(@Tags,@scopePath,@usedTags,@scopeList,@parser)
2929
toJson: () ->
3030
Tags:DocUtils.clone @scopeManager.tags
3131
DocxGen:@DocxGen
3232
intelligentTagging:DocUtils.clone @intelligentTagging
3333
scopePath:DocUtils.clone @scopeManager.scopePath
34+
scopeList: DocUtils.clone @scopeManager.scopeList
3435
usedTags:@scopeManager.usedTags
3536
localImageCreator:@localImageCreator
3637
imageId:@imageId
@@ -177,6 +178,7 @@ module.exports=class XmlTemplater #abstract class !!
177178
if argOptions?
178179
if argOptions.Tags?
179180
options.Tags=argOptions.Tags
181+
options.scopeList = options.scopeList.concat(argOptions.Tags)
180182
options.scopePath= options.scopePath.concat(@templaterState.loopOpen.tag)
181183
subfile= new @currentClass innerTagsContent,options
182184
subsubfile=subfile.applyTags()

examples/tagLoopExample.docx

-785 Bytes
Binary file not shown.

gulpfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var server=null;
1111

1212
try {
1313
var Blink1 = require('node-blink1');
14-
var blink1 = new Blink1('20002C4A');
14+
var blink1 = new Blink1();
1515
} catch (e) {
1616
blink1={
1717
fadeToRGB:function(){

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "docxtemplater",
3-
"version": "0.7.6",
3+
"version": "0.7.7",
44
"author": "Edgar Hipp",
55
"description": "A docX templater",
66
"contributors": [

0 commit comments

Comments
 (0)