Last active
February 20, 2020 03:38
-
-
Save bduggan/34407a7ccc96e638e8c5d9d26e625e11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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>' )); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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