Harnocode
Reformats JavaScript code to look like an ASCII-art picture of your choice. Like this:
const esprima =
require('esprima'); const escodegen =
require( 'escodegen') ; const fs=require('fs');
exports.harnocode=function( code,mask,options){options=
options||{ };const format= { safeConcatenation : true ,
escapeless:true,semicolons:true,renumber:true,quotes:'single'}
;code=escodegen.generate(esprima.parse(code),{format:format});
const tokens=tokenize(code);const lines=splitMaskToGroups(mask
);let tokenIndex= 0; let groupIndex =0; let result =[ ];let
splitStrings=options. splitStrings===undefined?false: options.
splitStrings;let safe=options.safe===undefined?true:options.
safe;function processMask(){return lines.map(groups=>{let
lineResult=[ ];let offset= 0;if( tokenIndex>=tokens .
length)return ;groups .forEach (( group,i )=> {let
groupWidth=group[0].length;let isBeforeNewline=i
==groups.length -1;if( group.index<offset ){
groupWidth -= offset- group .index ; if(
groupWidth <= 0 ) return ; } let
groupTokens=takeTokens (tokens ,
groupWidth , tokenIndex , {
isBeforeNewline , safe ,
splitStrings}); let
groupTokensJustified
= justify (
groupTokens
,
The code should stay functional after the transformation.
"Harnocode" (ukr. гарнокод) means "beautiful code" in the Ukrainian language.
Examples:
Here are functional copies of harnocode's source code formatted with itself:
- Hearty harnocode
- Ubuntu harnocode
- Apple's fan harnocode
- Ukrainian harnocode
- Yin and Yang harnocode
- DNA harnocode
Installation
$ npm install -g harnocode
Usage
Command-line:
$ harnocode ./examples/lodash.heart.js --mask masks/ubuntu.txt --split-long-lines > lodash.ubuntu.js
See
$ harnocode
for the full list of available options.
Node.js:
TODO
Converting images to masks
TODO
About
Harnocode v1 was written in ClojureScript during the DOU Hackathon on 21-22 February, 2015.
Harnocode v2 is a rewrite in a pure JavaScript. It adds support for ECMAScript 2019, npm module and a command-line tool.