Skip to content

Commit

Permalink
Fix multiline comments in all output formats
Browse files Browse the repository at this point in the history
Registers the following handlebars helpers:

* _cstylecomment_ marks text as C-Style comment
* _commoncomment_ line count based: applies C-Style or single line: '//'
* _indent (size=2)_ indents lines with 2 spaces (default size)

Relates to #139
  • Loading branch information
iilei committed Mar 22, 2018
1 parent 7aa8a53 commit ae2cfa3
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 14 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ theo.registerFormat(
// Source: {{stem meta.file}}
module.exports = [
{{#each props as |prop|}}
{{#if prop.comment}}// {{{prop.comment}}}{{/if}}
{{#if prop.comment}}
{{{trimLeft (indent (commoncomment (trim prop.comment)) 4)}}}
{{/if}}
['{{camelcase prop.name}}', '{{prop.value}}'],
{{/each}}
]
Expand Down
2 changes: 1 addition & 1 deletion lib/formats/cssmodules.css.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{#each props as |prop|}}
{{#if prop.comment~}}
/* {{{prop.comment}}} */
{{{trimLeft (indent (cstylecomment (trim prop.comment)))}}}
{{/if~}}
@value {{kebabcase prop.name}}: {{#eq prop.type "string"}}"{{/eq}}{{{prop.value}}}{{#eq prop.type "string"}}"{{/eq}};
{{/each}}
2 changes: 1 addition & 1 deletion lib/formats/custom-properties.css.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
:root {
{{#each props as |prop|}}
{{#if prop.comment}}
/* {{{prop.comment}}} */
{{{trimLeft (indent (cstylecomment (trim prop.comment)))}}}
{{/if}}
--{{kebabcase prop.name}}: {{#eq prop.type "string"}}"{{/eq}}{{{prop.value}}}{{#eq prop.type "string"}}"{{/eq}};
{{/each}}
Expand Down
2 changes: 1 addition & 1 deletion lib/formats/default.sass.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{#each props as |prop|}}
{{#if prop.comment~}}
// {{{prop.comment}}}
{{{trimLeft (indent (commoncomment (trim prop.comment)))}}}
{{/if~}}
${{kebabcase prop.name}}: {{#eq prop.type "string"}}"{{/eq}}{{{prop.value}}}{{#eq prop.type "string"}}"{{/eq}} !default
{{/each}}
2 changes: 1 addition & 1 deletion lib/formats/default.scss.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{#each props as |prop|}}
{{#if prop.comment~}}
// {{{prop.comment}}}
{{{trimLeft (indent (commoncomment (trim prop.comment)))}}}
{{/if~}}
${{kebabcase prop.name}}: {{#eq prop.type "string"}}"{{/eq}}{{{prop.value}}}{{#eq prop.type "string"}}"{{/eq}} !default;
{{/each}}
2 changes: 1 addition & 1 deletion lib/formats/less.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{#each props as |prop|}}
{{#if prop.comment~}}
// {{{prop.comment}}}
{{{trimLeft (indent (commoncomment (trim prop.comment)))}}}
{{/if~}}
@{{kebabcase prop.name}}: {{#eq prop.type "string"}}"{{/eq}}{{{prop.value}}}{{#eq prop.type "string"}}"{{/eq}};
{{/each}}
2 changes: 1 addition & 1 deletion lib/formats/list.scss.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
${{stem meta.file}}-list: (
{{#each props as |prop|}}
{{#if prop.comment}}
// {{{prop.comment}}}
{{{trimLeft (indent (commoncomment (trim prop.comment)))}}}
{{/if}}
"{{kebabcase prop.name}}",
{{/each}}
Expand Down
2 changes: 1 addition & 1 deletion lib/formats/map.scss.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
${{stem meta.file}}-map: (
{{#each props as |prop|}}
{{#if prop.comment}}
// {{{prop.comment}}}
{{{trimLeft (indent (commoncomment (trim prop.comment)))}}}
{{/if}}
'{{kebabcase prop.name}}': ({{#eq prop.type "string"}}"{{/eq}}{{{prop.value}}}{{#eq prop.type "string"}}"{{/eq}}),
{{/each}}
Expand Down
2 changes: 1 addition & 1 deletion lib/formats/map.variables.scss.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
${{stem meta.file}}-map: (
{{#each props as |prop|}}
{{#if prop.comment}}
// {{{prop.comment}}}
{{{trimLeft (indent (commoncomment (trim prop.comment)))}}}
{{/if}}
'{{kebabcase prop.name}}': ${{kebabcase prop.name}},
{{/each}}
Expand Down
3 changes: 2 additions & 1 deletion lib/formats/module.js.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

const Immutable = require("immutable");
const _ = require("lodash");
const { commonComment } = require("../util");

module.exports = def => {
return def
.get("props")
.map(prop => {
let result = Immutable.List();
if (prop.has("comment")) {
result = result.push(`// ${prop.get("comment")}`);
result = result.push(commonComment(prop.get("comment").trim()));
}
const k = _.camelCase(prop.get("name"));
const v = JSON.stringify(prop.get("value"));
Expand Down
2 changes: 1 addition & 1 deletion lib/formats/sass.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{#each props as |prop|}}
{{#if prop.comment~}}
// {{{prop.comment}}}
{{{trimLeft (indent (commoncomment (trim prop.comment)))}}}
{{/if~}}
${{kebabcase prop.name}}: {{#eq prop.type "string"}}"{{/eq}}{{{prop.value}}}{{#eq prop.type "string"}}"{{/eq}}
{{/each}}
2 changes: 1 addition & 1 deletion lib/formats/scss.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{#each props as |prop|}}
{{#if prop.comment~}}
// {{{prop.comment}}}
{{{trimLeft (indent (commoncomment (trim prop.comment)))}}}
{{/if~}}
${{kebabcase prop.name}}: {{#eq prop.type "string"}}"{{/eq}}{{{prop.value}}}{{#eq prop.type "string"}}"{{/eq}};
{{/each}}
2 changes: 1 addition & 1 deletion lib/formats/styl.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{#each props as |prop|}}
{{#if prop.comment~}}
// {{{prop.comment}}}
{{{trimLeft (indent (commoncomment (trim prop.comment)))}}}
{{/if~}}
${{kebabcase prop.name}} = {{#eq prop.type "string"}}"{{/eq}}{{{prop.value}}}{{#eq prop.type "string"}}"{{/eq}}
{{/each}}
5 changes: 4 additions & 1 deletion lib/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const path = require("path");

const { isString, isFunction } = require("lodash");

const { kebabCase } = require("./util");
const { kebabCase, cStyleComment, commonComment, indent } = require("./util");

// //////////////////////////////////////////////////////////////////
// Helpers
Expand All @@ -21,6 +21,9 @@ require("handlebars-helpers")({
});

handlebars.registerHelper("kebabcase", kebabCase);
handlebars.registerHelper("cstylecomment", cStyleComment);
handlebars.registerHelper("commoncomment", commonComment);
handlebars.registerHelper("indent", indent);

// //////////////////////////////////////////////////////////////////
// Register
Expand Down

0 comments on commit ae2cfa3

Please sign in to comment.