Skip to content

Instantly share code, notes, and snippets.

@bduggan
Last active February 20, 2020 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bduggan/34407a7ccc96e638e8c5d9d26e625e11 to your computer and use it in GitHub Desktop.
Save bduggan/34407a7ccc96e638e8c5d9d26e625e11 to your computer and use it in GitHub Desktop.
#!/usr/bin/env raku
my @void = <area base br col command embed hr img input keygen link meta param source track wbr>;
grammar H {
rule TOP { <block> }
rule block {
| '<' <tag> '>' <block>* '</' $<tag> '>'
| '<' $<void>=[ @void '/'? ] '>'
| <text>
}
token text { <-[<]>+ }
token tag { <-[>]>+ }
}
my $width = 4;
multi prettiest(Array $block, :$indent) {
prettiest($_, :$indent ) for @$block;
}
multi prettiest(Match $/ where $<void>, :$indent) {
put "<$<void>>".indent($indent);
}
multi prettiest(Match $/, :$indent = -$width) {
put "<$_>".indent($indent) with $<tag>;
put "$_".indent($indent) with $<text>;
prettiest($_, :indent($indent + $width) ) with $<block>;
put "</$_>".indent($indent) with $<tag>;
}
prettiest(H.parse( '<html><p>hi!<br>there!</p></html>' ));
prettiest(H.parse( '<html><p>hi!<br/>there!</p></html>' ));
<html>
<p>
hi!
<br>
there!
</p>
</html>
<html>
<p>
hi!
<br/>
there!
</p>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment