function ExtractContentsBetweenMarkersForText()

in BuildTools/djscommon.js [165:216]


function ExtractContentsBetweenMarkersForText(content, contentOnly, isExclusion, startMarker, endMarker, callback) {
    /// <summary>
    /// Extracts the lines from the specified text between the start and end markers.
    /// </summary>
    /// <param name="content" type="String">Text to process.</param>
    /// <param name="contentOnly" type="Boolean">
    /// true to skip everything until it's found between markers, false to start including everything from the start.
    /// </param>
    /// <param name="isExclusion" type="Boolean">
    /// false if the 'extraction' means keeping the content; true if it means not excluding it from the result.
    /// </param>
    /// <param name="startMarker" type="String">Line content to match for content start.</param>
    /// <param name="endMarker" type="String">Line content to match for content end.</param>
    /// <param name="callback" type="Function" mayBeNull="true">
    /// If true, then this function is called for every line along with the inContent flag
    /// before the line is added; the called function may return a line
    /// to be added in its place, null to skip processing.
    /// </param>
    /// <returns type="String">The extracted content.</returns>

    var inContent = contentOnly === false;
    var lines = StringSplit(content, "\r\n");
    var result = [];
    var i, len;
    for (i = 0, len = lines.length; i < len; i++) {
        var line = lines[i];
        var contentStartIndex = line.indexOf(startMarker);
        if (inContent === false && contentStartIndex !== -1) {
            inContent = true;
            continue;
        }

        var contentEndIndex = line.indexOf(endMarker);
        if (inContent === true && contentEndIndex !== -1) {
            inContent = false;
            continue;
        }

        if (inContent !== isExclusion) {
            if (callback) {
                var callbackResult = callback(line, inContent);
                if (callbackResult !== null && callbackResult !== undefined) {
                    result.push(callbackResult);
                }
            } else {
                result.push(line);
            }
        }
    }

    return result.join("\r\n");
}