Giter Site home page Giter Site logo

dmytro-krekota / jlto Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 1.0 870 KB

Nodejs-based tool for optimizing Jinja like templates.

License: MIT License

JavaScript 100.00%
jinja2 jinja jinja2-templates nunjucks nunjucks-templates twig twig-templates liquid jinjava optimization

jlto's Introduction

Support Ukraine ๐Ÿ‡บ๐Ÿ‡ฆ

#StandWithUkraine

JLTO

CircleCI Coverage Join the chat at https://gitter.im/dmytro-krekota_jlto/Lobby

NPM

Jinja Like Templates Optimizer (JLTO) is a Nodejs-based tool for optimizing Jinja like templates.

Gulp tool for JLTO:

gulp-jlto

Supported template engines:

Available options:

  • expressionStart - symbols at the beginning of expressions
  • expressionEnd - symbols at the end of expressions
  • blockStart - symbols at the beginning of blocks
  • blockEnd - symbols at the end of blocks
  • commentStart - symbols at the beginning of comments
  • commentEnd - symbols at the beginning of comments
  • specialChars - special chars in blocks and expressions
  • cleanupBlocks - flag for optimize blocks
  • cleanupExpressions - flag for optimize expressions
  • removeComments - flag for removing comments
  • minifyHtml - flag for minifying html code with html-minifier
  • minifyHtmlOptions - options for html-minifier

See default values for above options here.

Usage

Simple example:

let jlto = require('jlto');
let template = `
{{ hello }}
{{   "<John   &   Paul> ?"     | escape   }}
{{ '2.7'   | round }}{%  if  product  %}Product exists.{%  endif  %}
`;
let optimizedTemplate = jlto.optimizeString(template);
// optimizedTemplate:
// `
//{{hello}}
//{{"<John   &   Paul> ?"|escape}}
//{{'2.7'|round}}{%if product%}Product exists.{%endif%}
// `

Example of using minifyHtml option:

let jlto = require('jlto');
let template = `
<div {% if id %}id="{{ id | escape('html_attr') }}"{% endif %} class="section-container {{ classes | join(' ') | html_attribute }}">
  <div class="section-writables">
    {% for writable in writables  %}
      {{ writable | write | raw }}
    {% endfor %}
  </div>
</div>`;
let optimizedTemplate = jlto.optimizeString(template, {minifyHtml: true});
// optimizedTemplate:
// `<div {%if id%} id="{{id|escape('html_attr')}}" {%endif%} class="section-container {{classes|join(' ')|html_attribute}}"><div class="section-writables"> {%for writable in writables%} {{writable|write|raw}} {%endfor%} </div></div>`

Example of "nunjucks" templates minification with the custom GruntJS task:

module.exports = (grunt) => {
  grunt.registerTask('min-nunjucks', 'Min nunjucks templates', () => {
    let jlto = require('jlto');
    let fs = require('fs');
    let glob = require('glob');
    let done = this.async();
    glob('./**/*.nunjucks.html', (error, files) => {
      files.forEach((filePath) => {
        let fileContent;
        fileContent = fs.readFileSync(filePath).toString();
        try {
          fileContent = jlto.optimizeString(fileContent, {minifyHtml: true});
          fs.writeFileSync(filePath, fileContent);
        } catch (ignored) {}
      });
      return done();
    });
  });
};

Tests

Unit tests are written using Mocha and Chai. To run, invoke npm test.

License

JLTO is available under the MIT license, see the LICENSE file for more information.

jlto's People

Contributors

dependabot[bot] avatar dmytro-krekota avatar gitter-badger avatar greenkeeper[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

gitter-badger

jlto's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.