Advertisement
Guest User

texttemplate

a guest
May 8th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 2.52 KB | None | 0 0
  1. import qbs.TextFile
  2.  
  3. Module {
  4.     property var dict: ({})
  5.     property string outputTag: "text"
  6.     property string outputFileName
  7.     FileTagger {
  8.         patterns: ["*.in"]
  9.         fileTags: ["texttemplate.input"]
  10.     }
  11.     Rule {
  12.         inputs: ["texttemplate.input"]
  13.         outputFileTags: [product.texttemplate.outputTag]
  14.         outputArtifacts: [
  15.             {
  16.                 fileTags: [input.texttemplate.outputTag],
  17.                 filePath: input.texttemplate.outputFileName || input.completeBaseName
  18.             }
  19.         ]
  20.         prepare: {
  21.             var cmd = new JavaScriptCommand();
  22.             cmd.silent = true;
  23.             cmd.sourceCode = function() {
  24.                 try {
  25.                     var src = new TextFile(input.filePath, TextFile.ReadOnly);
  26.                     var dst = new TextFile(output.filePath, TextFile.WriteOnly);
  27.                     var rex = /\${(\$|\w+)}/g;
  28.                     var match;
  29.                     while (!src.atEof()) {
  30.                         rex.lastIndex = 0;
  31.                         var line = src.readLine();
  32.                         var matches = [];
  33.                         while (match = rex.exec(line))
  34.                             matches.push(match);
  35.                         for (var i = matches.length; --i >= 0;) {
  36.                             match = matches[i];
  37.                             var replacement;
  38.                             if (match[1] === "$") {
  39.                                 replacement = "$";
  40.                             } else {
  41.                                 replacement = input.texttemplate.dict[match[1]];
  42.                                 if (typeof replacement === "undefined") {
  43.                                     throw new Error("Placeholder '" + match[1]
  44.                                                     + "' is not defined in textemplate.dict for '"
  45.                                                     + input.fileName + "'.");
  46.                                 }
  47.                             }
  48.                             line = line.substr(0, match.index)
  49.                                     + replacement
  50.                                     + line.substr(match.index + match[0].length);
  51.                         }
  52.                         dst.writeLine(line);
  53.                     }
  54.                 } finally {
  55.                     if (src)
  56.                         src.close();
  57.                     if (dst)
  58.                         dst.close();
  59.                 }
  60.             };
  61.             return [cmd];
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement