
'use strict';

const gulp = require('gulp');
const modify = require('gulp-modify');
const cheerio = require('cheerio');
const concat = require('gulp-concat');

gulp.task('icons', function() {
  return gulp.src(['assets/svg/*.svg'], {base: '.'})
    .pipe(modify({
      fileModifier: function(file, contents) {
        var id = file.path.replace(/.*\/(.*).svg/, '$1');
        var svg = cheerio.load(contents, {xmlMode: true})('svg');
        // Remove fill attributes.
        svg.children('[fill]').removeAttr('fill');
        // Output the "meat" of the SVG as group element.
        return '<g id="' + id + '">' + svg.children() + '</g>';
      }
    }))
    .pipe(concat('vaadin-icons.html'))
    .pipe(modify({
      fileModifier: function(file, contents) {
        // Enclose all icons in an iron-iconset-svg
        return `<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../iron-iconset-svg/iron-iconset-svg.html">
<iron-iconset-svg name="vaadin-icons" size="16">
<svg><defs>
` + contents + `
</defs></svg>
</iron-iconset-svg>
`;
      }
    }))
    .pipe(gulp.dest('.'));
});

// Generates an AsciiDoc table of all icons from the JSON metadata.
/* eslint-disable no-console */
gulp.task('docs:table', () => {
  const iconData = require('./assets/vaadin-font-icons.json');

  console.log('[width="100%", options="header"]');
  console.log('|======================');
  console.log('| Icon | Name | Ligature | Unicode | Categories | Tags');
  iconData.forEach((icon) => {
    const {name, code} = icon;
    const categories = icon.categories.join(', ');
    const meta = icon.meta.join(', ');
    console.log(`| image:../assets/png/${name}.png[] | [propertyname]#${name}# | ${name} | ${code} | ${categories} | ${meta}`);
  });
  console.log('|======================');
});
/* eslint-enable no-console */

@@ -750,32 +750,69 @@
 le, contents) %7B%0A
+        /* eslint-disable max-len */%0A
         // Enclo
@@ -866,169 +866,965 @@
 urn 
-%60%3Clink rel=%22import%22 href=%22../iron-icon/iron-icon.html%22%3E%0A%3Clink rel=%22import%22 href=%22../iron-iconset-svg/iron-iconset-svg.html%22%3E%0A%3Ciron-iconset-svg name=%22vaadin-icons
+/* html */%60%3C!-- NOTICE: Generated with 'gulp icons' --%3E%0A%3C!--%0A@license%0ACopyright (c) 2015-2017 Vaadin Ltd.%0AThis program is available under Apache License Version 2.0, available at https://vaadin.com/license/%0A--%3E%0A%0A%3Clink rel=%22import%22 href=%22../iron-icon/iron-icon.html%22%3E%0A%3Clink rel=%22import%22 href=%22../iron-iconset-svg/iron-iconset-svg.html%22%3E%0A%0A%3C!--%0A%5C%60vaadin-icons%5C%60 is a set of 600+ icons which can be used together with Polymer's %5B%5C%60iron-icon%5C%60%5D(https://elements.polymer-project.org/elements/iron-icon) component.%0A%0ATo use the %5C%60vaadin-icons%5C%60 iconset, import the specific %5C%60vaadin-icons.html%5C%60, and%0Aspecify the icon as %5C%60vaadin:%3Cicon%3E%5C%60. For example, to use a cart icon, you would%0Ause:%0A%5C%60%5C%60%5C%60html%0A%3Clink rel=%22import%22 href=%22/components/vaadin-icons/vaadin-icons.html%22%3E%0A%3Ciron-icon icon=%22vaadin:cart%22%3E%3C/iron-icon%3E%0A%5C%60%5C%60%5C%60%0A%0AFor the complete list of available icons, see https://vaadin.com/icons%0A@pseudoElement vaadin-icons%0A@demo demo/%0A--%3E%0A%3Ciron-iconset-svg name=%22vaadin
 %22 si
@@ -1898,16 +1898,52 @@
 svg%3E%0A%60;%0A
+        /* eslint-enable max-len */%0A
       %7D%0A

fix gulp icons task output