Skip to content

Commit

Permalink
Merge pull request #206 from veelo/master
Browse files Browse the repository at this point in the history
Few improvements to HTML output.
  • Loading branch information
PhilippeSigaud committed Mar 29, 2016
2 parents 8e6a53e + b2268e9 commit 6b99018
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pegged/peg.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import std.conv;
import std.string: strip;
import std.typetuple;

private string stringified(string inp)
package string stringified(string inp)
{
import std.format : format;
return format(`%(%s%)`, (&inp)[0..1]);
Expand Down
47 changes: 31 additions & 16 deletions pegged/tohtml.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module pegged.tohtml;

import std.stdio;
import std.conv;
import std.algorithm.searching;
import pegged.peg;

void toHTML(const ref ParseTree p, File file)
Expand All @@ -13,16 +14,20 @@ void toHTML(const ref ParseTree p, File file)
<meta charset="UTF-8">
<title>Pegged produced parse tree</title>
<style>
a.tooltip {
code {
position: relative;
font-family: monospace;
}
white-space: pre;
}
code.failure {
color: red;
}
/* hide tooltip */
a.tooltip span {
code span {
display: none;
}
/* show and style tooltip */
a.tooltip:hover span {
code:hover span {
/* show tooltip */
display: block;
/* position relative to container div.tooltip */
Expand All @@ -35,10 +40,14 @@ a.tooltip:hover span {
border: 0.1em solid #b7ddf2;
border-radius: 0.5em;
white-space: nowrap;
z-index : 1;
z-index: 1;
}
details, div {
details {
margin-left:25px;
white-space: nowrap;
}
details.leaf summary::-webkit-details-marker {
opacity: 0;
}
</style>
</head>
Expand All @@ -47,16 +56,23 @@ details, div {

string treeToHTML(const ref ParseTree p)
{
import std.algorithm.comparison;
import std.algorithm.searching;
string summary = p.name ~ " " ~ to!string([p.begin, p.end]);
auto firstNewLine = p.input[p.begin .. p.end].countUntil('\n');
if (p.begin != p.end)
summary ~= ` <a class="tooltip">` ~ p.input[p.begin .. firstNewLine >= 0 ? p.begin + firstNewLine : p.end] ~
"<span><pre>" ~ p.input[p.begin .. p.end] ~ "</pre></span></a>";
if (p.children.length == 0)
return "<div>" ~ summary ~ "</div>\n";
string result = "<details><summary>" ~ summary ~ "</summary>\n";
string summary = p.name ~ " " ~ to!string([p.begin, p.end]);
if (p.children.length == 0 && !p.successful)
{
Position pos = position(p);
string left = pos.index < 10 ? p.input[0 .. pos.index] : p.input[pos.index-10 .. pos.index];
string right = pos.index + 10 < p.input.length ? p.input[pos.index .. pos.index + 10] : p.input[pos.index .. $];
summary ~= " failure at line " ~ to!string(pos.line) ~ ", col " ~ to!string(pos.col) ~ ", "
~ (left.length > 0 ? "after <code>" ~ left.stringified ~ "</code> " : "")
~ "expected <code>" ~ (p.matches.length > 0 ? p.matches[$-1].stringified : "NO MATCH")
~ `</code>, but got <code class="failure">` ~ right.stringified ~ "</code>\n";
}
else
summary ~= " <code" ~ (!p.successful ? ` class="failure"` : "") ~ ">"
~ p.input[p.begin .. firstNewLine >= 0 ? p.begin + firstNewLine : p.end]
~ "<span><pre>" ~ p.input[p.begin .. p.end] ~ "</pre></span></code>";
string result = "<details" ~ (p.children.length == 0 ? ` class="leaf"` : "") ~ "><summary>" ~ summary ~ "</summary>\n";
foreach (child; p.children)
result ~= treeToHTML(child);
return result ~ "</details>\n";
Expand All @@ -72,7 +88,6 @@ details, div {

void toHTML(const ref ParseTree p, string filename)
{
import std.algorithm.searching;
if (filename.endsWith(".html", ".htm") == 0)
filename ~= ".html";
toHTML(p, File(filename, "w"));
Expand Down

0 comments on commit 6b99018

Please sign in to comment.