function buildRegionDocs()

in tools/doc-shredder/regionExtractor.js [28:97]


function buildRegionDocs(content, extn) {
  var commentInfo = getCommentInfo(extn);
  if (!commentInfo) {
    return  [ { content: content } ];
  }

  var lines = result = content.split(/\r?\n/);

  var docStack = []; // items will be both popped and removed from the middle
  var docMap = {};
  var docPlaster = '. . .';
  var doc;
  var regionNames;
  lines.forEach(function(line, ix) {
    if (isCommentLine(line, commentInfo.prefix)) {
      if (hasRegionTag(line)) {
        lines[ix] = nullLine;
        regionNames = getRegionNames(line);
        regionNames.forEach(function(rn) {
          doc = docMap[rn];
          if (!doc) {
            // regionName may be ''
            doc = {regionName: rn, ranges: [ { startIx: ix} ] };
            docMap[rn] = doc;
          } else {
            // only add a new range if prev range is closed
            var lastRange = doc.ranges[doc.ranges.length-1];
            if (lastRange.endIx) {
              doc.ranges.push({startIx: ix});
            }
          }
          docStack.push(doc);
        });

      } else if (hasEndRegionTag(line)) {
        lines[ix] = nullLine;
        regionNames = getEndRegionNames(line);
        regionNames.forEach(function(rn) {
          // handle endregions with no name specially.
          // They operate on the last region created.
          if (rn.length == 0) {
            if (docStack.length) {
              // update last item on the stack
              doc = docStack.pop();
              doc.ranges[doc.ranges.length - 1].endIx = ix;
            }
          } else {
            doc = docMap[rn];
            // ignore endregion if name is specified but not found.
            if (doc) {
              doc.ranges[doc.ranges.length - 1].endIx = ix;
              // remove doc from stack
              _.remove(docStack, function (item) {
                return item.regionName === rn;
              });
            }
          }
        });
      } else if (hasDocPlasterTag(line)) {
        line[ix] = nullLine;
        docPlaster =  getDocPlaster(line);
      }
    }
  });

  var docs = _.values(docMap);
  var plasterComment = docPlaster && commentInfo.plasterPattern.replace('{tag}', docPlaster);
  docs = reattachDocs(docs, lines, plasterComment);
  return docs;
}