├── .gitignore ├── package.json ├── src ├── index.js ├── helpers.js └── cli.js ├── readme.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vsc-snippets-html2pug", 3 | "version": "0.1.0", 4 | "description": "A tool to automatically generate VSCode Pug snippets out of HTML ones.", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/Abdelaziz18003/vsc-snippets-html2pug.git" 8 | }, 9 | "main": "src/index.js", 10 | "bin": { 11 | "vsc-snippets-html2pug": "src/cli.js" 12 | }, 13 | "scripts": { 14 | "test": "echo \"Error: no test specified\" && exit 1" 15 | }, 16 | "author": "Abdelaziz Mokhnache", 17 | "license": "MIT", 18 | "dependencies": { 19 | "html2pug": "^4.0.0" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | const fs = require('fs'); 4 | const { transformSnippets, writeSnippetsFile } = require('./helpers'); 5 | 6 | /** 7 | * Generate VSCode Pug snippets out of html ones 8 | * 9 | * @param {Object} options 10 | * Options object, required fields are `src` and `dist`. 11 | */ 12 | 13 | function generatePugSnippets (options) { 14 | if (options.src && options.dist) { 15 | const htmlSnippets = fs.readFileSync(options.src, {encoding: 'utf-8'}); 16 | const pugSnippets = transformSnippets(htmlSnippets); 17 | writeSnippetsFile(options.dist, pugSnippets); 18 | } else { 19 | throw new Error('Missing required arguments'); 20 | } 21 | } 22 | 23 | module.exports = generatePugSnippets 24 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # vsc-snippets-html2pug 2 | 3 | A tool to automatically generate VSCode Pug snippets out of HTML ones. 4 | 5 | ## Installation 6 | 7 | ```bash 8 | npm install vsc-snippets-html2pug 9 | ``` 10 | 11 | ## Usage 12 | 13 | ### From CLI 14 | 15 | ```bash 16 | vsc-snippets-html2pug options 17 | 18 | # example 19 | vsc-snippets-html2pug --src ./html-snippets.json --dist ./pug-snippets.json 20 | ``` 21 | 22 | ### From JS file 23 | 24 | ```js 25 | const generatePugSnippets = require('vsc-snippets-html2pug'); 26 | const options = { 27 | src: './html-snippets.json', 28 | dist: './pug-snippets.json' 29 | } 30 | 31 | generatePugSnippets(options); 32 | ``` 33 | 34 | ## Options 35 | 36 | - ***--src***: HTML snippets file path. 37 | - ***--dist***: Pug snippets file path that will be created -------------------------------------------------------------------------------- /src/helpers.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const html2pug = require('html2pug'); 3 | 4 | function fixQuotes (array) { 5 | return array.map(string => { 6 | return string.replace(/\\'/g, "'") 7 | }) 8 | } 9 | 10 | function transformSnippets (htmlSnippets) { 11 | let pugSnippets = JSON.parse(htmlSnippets); 12 | for (let key in pugSnippets) { 13 | let htmlBody = pugSnippets[key].body.join(' ').replace(/\t/g, ''); 14 | let pugBody = html2pug(htmlBody, { tabs: true, fragment: true }); 15 | pugSnippets[key].body = fixQuotes(pugBody.split('\n')); 16 | } 17 | return JSON.stringify(pugSnippets, null, 2); 18 | } 19 | 20 | function writeSnippetsFile (path, snippets) { 21 | fs.writeFileSync(path, snippets); 22 | console.log( 23 | '------------------------------------------\n' + 24 | 'Pug Snippets file was created successfully\n' + 25 | `${path}\n` + 26 | '------------------------------------------' 27 | ); 28 | } 29 | 30 | module.exports = { 31 | fixQuotes, 32 | transformSnippets, 33 | writeSnippetsFile 34 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 Abdelaziz Mokhnache 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /src/cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | "use strict"; 4 | const generatePugSnippets = require('./index'); 5 | 6 | const cliOptions = ['--src', '--dist']; 7 | const help = ` 8 | A tool to automatically generate Pug vscode snippets from HTML ones. 9 | 10 | Usage: 11 | vsc-snippets-html2pug options 12 | 13 | Options: 14 | --src: HTML snippets file path. 15 | --dist: Pug snippets file path that should be created 16 | 17 | Example: 18 | vsc-snippets-html2pug --src ./html-snippets.json --dist ./pug-snippets.json 19 | `; 20 | 21 | function printHelp () { 22 | console.log(help); 23 | } 24 | 25 | function getOptionValue (optionName) { 26 | const optionNameIndex = process.argv.indexOf(optionName); 27 | const optionValueIndex = optionNameIndex + 1; 28 | return optionValueIndex ? process.argv[optionValueIndex] : undefined; 29 | } 30 | 31 | function parseArgv () { 32 | let options = {} 33 | cliOptions.forEach(optionName => { 34 | const optionValue = getOptionValue(optionName); 35 | if (optionValue) { 36 | options[optionName.replace('--', '')] = optionValue; 37 | } 38 | }) 39 | return options; 40 | } 41 | 42 | if (process.argv.length === 2 || process.argv.includes('help')) { 43 | printHelp() 44 | } else { 45 | const options = parseArgv(); 46 | generatePugSnippets(options); 47 | } 48 | --------------------------------------------------------------------------------