<commit_before>// Dependencies that makes this all work! Yay packages!
var gulp = require('gulp'),
    cleanCSS = require('gulp-clean-css'),
    del = require('del'),
    dotenv_extended = require('dotenv-extended'),
    fs = require('fs'),
    git_rev = require('git-rev'),
    inlineCSS = require('gulp-inline-css'),
    preprocess = require('gulp-preprocess'),
    rename = require('gulp-rename'),
    strip = require('gulp-strip-comments');

// Load in environment variables from .boilerplate.defaults first and then .boilerplate.custom if it exists
var boilerplateConfig = dotenv_extended.load(
    {
        encoding: 'utf8',
        silent: true,
        path: '.boilerplate.custom',
        defaults: '.boilerplate.defaults',
        errorOnMissing: false,
        errorOnExtra: false,
        assignToProcessEnv: true,
        overrideProcessEnv: true
    }
);

// We need to check if DOCTYPE_VERSION is using an XHTML value for inline-css purposes later
var currentSetDoctype = process.env.DOCTYPE_VERSION;
if (/XHTML/i.test(currentSetDoctype)) {
    xmlMode = true;
} 
else {
    xmlMode = false;
}

// Clean out /tmp and /dist directories each time gulp is run
gulp.task('clean', function () {
    return del(
        [
            'tmp/css/**', 
            'tmp/html-samples/**',
            'dist/boilerplate/**'
        ]
    ).then(paths => {
        console.log('Cleaning directories :\n', paths.join('\n'));
    });
});

// Get Git revision details for use in boilerplate templates
gulp.task('git-rev-info', function() {
    // Get latest commit (short)
    git_rev.short(
        function(rev) {
            return process.env.GIT_REVISION_SHORT = rev;
        }
    )
    // Get latest commit (long)
    git_rev.long(
        function(rev) {
            return process.env.GIT_REVISION_LONG = rev;
        }
    )
    // Get branch name boilerplate is being generated from
    git_rev.branch(
        function(branch) {
            return process.env.GIT_BRANCH = branch;      
        }
    )
});

// Preprocess CSS files with config values
gulp.task('preprocess-css', function() {
    var stream = gulp.src('./app/css/*.css')
    .pipe(preprocess())
    .pipe(gulp.dest('./tmp/css/'))
    return stream;
});

// Minify processed CSS for inclusion in the email boilerplate
gulp.task('minify-css', ['preprocess-css'], function() {
    var stream = gulp.src('./tmp/css/*.css')
    .pipe(cleanCSS(
        {
            advanced: false,
            keepSpecialComments: '*',
            aggressiveMerging: false,
            debug: true, // Needed for the size output on before and after min files
            keepBreaks: true,
            shorthandCompacting: false,
            compatibility: '*,' + 
            '-properties.colors,' +
            '-properties.zeroUnits'
        },
        // Log original size and minified size in gulp output
        function(details) {
            console.log(details.name + ': ' + details.stats.originalSize);
            console.log(details.name + '.min' + ': ' + details.stats.minifiedSize);
        })
    )
    .pipe(rename({ extname: '.css.min' }))
    .pipe(gulp.dest('./tmp/css/'));
    return stream;
});

// Remove CSS comments from minified files for production version of the boilerplate
gulp.task('remove-css-comments', ['minify-css'], function() {
    var stream = gulp.src('./tmp/css/*.css.min')
    .pipe(cleanCSS(
        { 
            advanced: false,
            aggressiveMerging: false,
            keepBreaks: true,
            keepSpecialComments: 0,
            shorthandCompacting: false,
            compatibility: '*,' + 
            '-properties.colors,' +
            '-properties.zeroUnits'
        }
    ))
    .pipe(rename(
        function(path) { 
            path.extname = "" // Drop extension to avoid double css naming
        }
    ))
    // Specific versions with no CSS comments for the production version
    .pipe(rename({ extname: '-nocomments.css.min'}))
    .pipe(gulp.dest('./tmp/css/'));
    return stream;
});

// Build all HTML samples, inline CSS and strip comments to be included in boilerplate
gulp.task('build-html-samples', function() {
    var stream = gulp.src('./app/html-samples/*.html')
    .pipe(preprocess())
    .pipe(inlineCSS(
        {
            applyStyleTags: true,
            removeStyleTags: true,
            removeHtmlSelectors: true,
            applyTableAttributes: true,
            xmlMode: xmlMode
        }
    ))
    .pipe(gulp.dest('./tmp/html-samples/'));
    return stream;
});

// Strip HTML comments from HTML samples for to be included in boilerplate
gulp.task('remove-html-comments', ['build-html-samples'], function() {
    var stream = gulp.src('./tmp/html-samples/*.html')
    .pipe(strip(
        {
            ignore: /<!--\[if(?!\s*(?:\[[^\]]+]|<!|>))(?:(?!-->).)*-->/g, // Mantain MSO conditional comments
            safe: true,
            trim: true
        }
    ))
    .pipe(rename({ extname: '-nocomments.html'}))
    .pipe(gulp.dest('./tmp/html-samples/'));
    return stream;
});

// Build boilerplate and output the HTML versions
gulp.task('preprocess-boilerplate', ['remove-html-comments'], function() {
    var stream = gulp.src('app/email-boilerplate*')
    .pipe(preprocess({ extension: 'html' }))
    .pipe(rename(
        function(path) { 
            path.extname = "" // Drop preprocess ext and change to .html
        }
    ))
    .pipe(gulp.dest('./dist/boilerplate/'))
    return stream;
});

// Inline CSS to main layout elements after boilerplate is generated and update documents
gulp.task('inline-css', ['preprocess-boilerplate'], function() {
    var stream = gulp.src('./dist/boilerplate/*.html')
    .pipe(inlineCSS(
        {
            applyTableAttributes: true,
            applyWidthAttributes: true,
            applyStyleTags: false,
            applyLinkTags: true,
            removeStyleTags: false,
            removeLinkTags: true,
            xmlMode: xmlMode
        }
    ))
    .pipe(gulp.dest('./dist/boilerplate/'));
    return stream;
});

// Detect certain configurations and warn in console when they are not optimal from recommended guidelines
gulp.task('check-config', ['inline-css'], function() {

    var isMsoNamespacesEnabled = process.env.ENABLE_VML_NAMESPACES;
    var isXuaCompatMetaTagEnabled = process.env.ENABLE_XUA_COMPATIBLE_META_TAG;
    var xuaCompatValue = process.env.XUA_COMPATIBLE_VALUE;
    var isViewportMetaTagEnabled = process.env.ENABLE_VIEWPORT_META_TAG;
    var viewportValue = process.env.VIEWPORT_META_VALUE;
    var isFormatDetectionEnabled = process.env.ENABLE_FORMAT_DETECTION_META_TAG;
    var formatDetectionValue = process.env.FORMAT_DETECTION_META_VALUE;
    var isYahooShortcutsCSSEnabled = process.env.ENABLE_YAHOO_SHORTCUTS_CSS;
    var isMsoDpiScalingFixEnabled = process.env.ENABLE_MSO_DPI_SCALING_FIX;
    var isWebkitMediaQueryEnabled = process.env.ENABLE_WEBKIT_DETECTION_QUERY;
    var isGeckoMediaQueryEnabled = process.env.ENABLE_GECKO_MEDIA_QUERY;<commit_msg>Change var to check for XHTML doctype value<commit_after>// Dependencies that makes this all work! Yay packages!
var gulp = require('gulp'),
    cleanCSS = require('gulp-clean-css'),
    del = require('del'),
    dotenv_extended = require('dotenv-extended'),
    fs = require('fs'),
    git_rev = require('git-rev'),
    inlineCSS = require('gulp-inline-css'),
    preprocess = require('gulp-preprocess'),
    rename = require('gulp-rename'),
    strip = require('gulp-strip-comments');

// Load in environment variables from .boilerplate.defaults first and then .boilerplate.custom if it exists
var boilerplateConfig = dotenv_extended.load(
    {
        encoding: 'utf8',
        silent: true,
        path: '.boilerplate.custom',
        defaults: '.boilerplate.defaults',
        errorOnMissing: false,
        errorOnExtra: false,
        assignToProcessEnv: true,
        overrideProcessEnv: true
    }
);

// We need to check if DOCTYPE_VERSION is using an XHTML value for inline-css purposes later
var currentSetDoctype = process.env.DOCTYPE_VERSION;
if (/xhtml/i.test(currentSetDoctype)) {
    isXHTMLDoctype = true;
} 
else {
    isXHTMLDoctype = false;
}

// Clean out /tmp and /dist directories each time gulp is run
gulp.task('clean', function () {
    return del(
        [
            'tmp/css/**', 
            'tmp/html-samples/**',
            'dist/boilerplate/**'
        ]
    ).then(paths => {
        console.log('Cleaning directories :\n', paths.join('\n'));
    });
});

// Get Git revision details for use in boilerplate templates
gulp.task('git-rev-info', function() {
    // Get latest commit (short)
    git_rev.short(
        function(rev) {
            return process.env.GIT_REVISION_SHORT = rev;
        }
    )
    // Get latest commit (long)
    git_rev.long(
        function(rev) {
            return process.env.GIT_REVISION_LONG = rev;
        }
    )
    // Get branch name boilerplate is being generated from
    git_rev.branch(
        function(branch) {
            return process.env.GIT_BRANCH = branch;      
        }
    )
});

// Preprocess CSS files with config values
gulp.task('preprocess-css', function() {
    var stream = gulp.src('./app/css/*.css')
    .pipe(preprocess())
    .pipe(gulp.dest('./tmp/css/'))
    return stream;
});

// Minify processed CSS for inclusion in the email boilerplate
gulp.task('minify-css', ['preprocess-css'], function() {
    var stream = gulp.src('./tmp/css/*.css')
    .pipe(cleanCSS(
        {
            advanced: false,
            keepSpecialComments: '*',
            aggressiveMerging: false,
            debug: true, // Needed for the size output on before and after min files
            keepBreaks: true,
            shorthandCompacting: false,
            compatibility: '*,' + 
            '-properties.colors,' +
            '-properties.zeroUnits'
        },
        // Log original size and minified size in gulp output
        function(details) {
            console.log(details.name + ': ' + details.stats.originalSize);
            console.log(details.name + '.min' + ': ' + details.stats.minifiedSize);
        })
    )
    .pipe(rename({ extname: '.css.min' }))
    .pipe(gulp.dest('./tmp/css/'));
    return stream;
});

// Remove CSS comments from minified files for production version of the boilerplate
gulp.task('remove-css-comments', ['minify-css'], function() {
    var stream = gulp.src('./tmp/css/*.css.min')
    .pipe(cleanCSS(
        { 
            advanced: false,
            aggressiveMerging: false,
            keepBreaks: true,
            keepSpecialComments: 0,
            shorthandCompacting: false,
            compatibility: '*,' + 
            '-properties.colors,' +
            '-properties.zeroUnits'
        }
    ))
    .pipe(rename(
        function(path) { 
            path.extname = "" // Drop extension to avoid double css naming
        }
    ))
    // Specific versions with no CSS comments for the production version
    .pipe(rename({ extname: '-nocomments.css.min'}))
    .pipe(gulp.dest('./tmp/css/'));
    return stream;
});

// Build all HTML samples, inline CSS and strip comments to be included in boilerplate
gulp.task('build-html-samples', function() {
    var stream = gulp.src('./app/html-samples/*.html')
    .pipe(preprocess())
    .pipe(inlineCSS(
        {
            applyStyleTags: true,
            removeStyleTags: true,
            removeHtmlSelectors: true,
            applyTableAttributes: true,
            xmlMode: isXHTMLDoctype
        }
    ))
    .pipe(gulp.dest('./tmp/html-samples/'));
    return stream;
});

// Strip HTML comments from HTML samples for to be included in boilerplate
gulp.task('remove-html-comments', ['build-html-samples'], function() {
    var stream = gulp.src('./tmp/html-samples/*.html')
    .pipe(strip(
        {
            ignore: /<!--\[if(?!\s*(?:\[[^\]]+]|<!|>))(?:(?!-->).)*-->/g, // Mantain MSO conditional comments
            safe: true,
            trim: true
        }
    ))
    .pipe(rename({ extname: '-nocomments.html'}))
    .pipe(gulp.dest('./tmp/html-samples/'));
    return stream;
});

// Build boilerplate and output the HTML versions
gulp.task('preprocess-boilerplate', ['remove-html-comments'], function() {
    var stream = gulp.src('app/email-boilerplate*')
    .pipe(preprocess({ extension: 'html' }))
    .pipe(rename(
        function(path) { 
            path.extname = "" // Drop preprocess ext and change to .html
        }
    ))
    .pipe(gulp.dest('./dist/boilerplate/'))
    return stream;
});

// Inline CSS to main layout elements after boilerplate is generated and update documents
gulp.task('inline-css', ['preprocess-boilerplate'], function() {
    var stream = gulp.src('./dist/boilerplate/*.html')
    .pipe(inlineCSS(
        {
            applyTableAttributes: true,
            applyWidthAttributes: true,
            applyStyleTags: false,
            applyLinkTags: true,
            removeStyleTags: false,
            removeLinkTags: true,
            xmlMode: isXHTMLDoctype
        }
    ))
    .pipe(gulp.dest('./dist/boilerplate/'));
    return stream;
});

// Detect certain configurations and warn in console when they are not optimal from recommended guidelines
gulp.task('check-config', ['inline-css'], function() {

    var isMsoNamespacesEnabled = process.env.ENABLE_VML_NAMESPACES;
    var isXuaCompatMetaTagEnabled = process.env.ENABLE_XUA_COMPATIBLE_META_TAG;
    var xuaCompatValue = process.env.XUA_COMPATIBLE_VALUE;
    var isViewportMetaTagEnabled = process.env.ENABLE_VIEWPORT_META_TAG;
    var viewportValue = process.env.VIEWPORT_META_VALUE;
    var isFormatDetectionEnabled = process.env.ENABLE_FORMAT_DETECTION_META_TAG;
    var formatDetectionValue = process.env.FORMAT_DETECTION_META_VALUE;
    var isYahooShortcutsCSSEnabled = process.env.ENABLE_YAHOO_SHORTCUTS_CSS;
    var isMsoDpiScalingFixEnabled = process.env.ENABLE_MSO_DPI_SCALING_FIX;
    var isWebkitMediaQueryEnabled = process.env.ENABLE_WEBKIT_DETECTION_QUERY;
    var isGeckoMediaQueryEnabled = process.env.ENABLE_GECKO_MEDIA_QUERY;
{'fraction_non_alphanumeric': 0.13220675944333996, 'fraction_numerical': 0.00028401022436807724, 'mean_word_length': 2.4933035714285716, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '166636', 'n_tokens_mistral': 4317, 'n_tokens_neox': 4195, 'n_words': 952}