|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const logger = require('./log'); |
| 4 | +const ph = require('./parameter_hunter'); |
| 5 | +const smh = require('./style_modifier_hunter'); |
| 6 | +const jsonCopy = require('./json_copy'); |
| 7 | +const getPartial = require('./get'); |
| 8 | + |
| 9 | +const parameter_hunter = new ph(); |
| 10 | +const style_modifier_hunter = new smh(); |
| 11 | + |
| 12 | +module.exports = function (currentPattern, patternlab) { |
| 13 | + |
| 14 | + const processRecursive = require('./processRecursive'); |
| 15 | + |
| 16 | + //find how many partials there may be for the given pattern |
| 17 | + const foundPatternPartials = currentPattern.findPartials(); |
| 18 | + |
| 19 | + // expand any partials present in this pattern; that is, drill down into |
| 20 | + // the template and replace their calls in this template with rendered |
| 21 | + // results |
| 22 | + if (currentPattern.engine.expandPartials && (foundPatternPartials !== null && foundPatternPartials.length > 0)) { |
| 23 | + |
| 24 | + logger.debug(`found partials for ${currentPattern.patternPartial}`); |
| 25 | + |
| 26 | + // determine if the template contains any pattern parameters. if so they |
| 27 | + // must be immediately consumed |
| 28 | + return parameter_hunter.find_parameters(currentPattern, patternlab).then(() => { |
| 29 | + |
| 30 | + //do something with the regular old partials |
| 31 | + foundPatternPartials.forEach((foundPartial) => { |
| 32 | + |
| 33 | + var partial = currentPattern.findPartial(foundPartial); |
| 34 | + var partialPattern = getPartial(partial, patternlab); |
| 35 | + |
| 36 | + //recurse through nested partials to fill out this extended template. |
| 37 | + return processRecursive(partialPattern.relPath, patternlab).then(() => { //eslint-disable-line no-loop-func |
| 38 | + |
| 39 | + //complete assembly of extended template |
| 40 | + //create a copy of the partial so as to not pollute it after the getPartial call. |
| 41 | + var cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partial}`); |
| 42 | + |
| 43 | + //if partial has style modifier data, replace the styleModifier value |
| 44 | + if (currentPattern.stylePartials && currentPattern.stylePartials.length > 0) { |
| 45 | + style_modifier_hunter.consume_style_modifier(cleanPartialPattern, foundPartial, patternlab); |
| 46 | + } |
| 47 | + |
| 48 | + //this is what we came here for |
| 49 | + logger.debug(`within ${currentPattern.patternPartial}, replacing extendedTemplate partial ${foundPartial} with ${cleanPartialPattern.patternPartial}'s extendedTemplate`); |
| 50 | + |
| 51 | + currentPattern.extendedTemplate = currentPattern.extendedTemplate.replace(foundPartial, cleanPartialPattern.extendedTemplate); |
| 52 | + |
| 53 | + // update the extendedTemplate in the partials object in case this |
| 54 | + // pattern is consumed later |
| 55 | + patternlab.partials[currentPattern.patternPartial] = currentPattern.extendedTemplate; |
| 56 | + |
| 57 | + return Promise.resolve(); |
| 58 | + }).catch(reason => { |
| 59 | + console.log(reason); |
| 60 | + logger.error(reason); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + }).catch(reason => { |
| 65 | + console.log(reason); |
| 66 | + logger.error(reason); |
| 67 | + }); |
| 68 | + } |
| 69 | + return Promise.resolve(); |
| 70 | +}; |
0 commit comments