Skip to content

Commit

Permalink
[STD_syntax_highlight] no DumpMatch; #it is been 'nice' seeing ya
Browse files Browse the repository at this point in the history
[STD_syntax_highlight] no more javascript tree traversal; tree info is hidden in html 
[STD_syntax_highlight] You can consider this an SP1 ;-)


git-svn-id: http://svn.pugscode.org/pugs@22838 c213334d-75ef-0310-aa23-eaa082d1ae64
  • Loading branch information
azawawi committed Oct 31, 2008
1 parent 7db6ac1 commit d7ee3e3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 56 deletions.
88 changes: 54 additions & 34 deletions STD_syntax_highlight
Expand Up @@ -13,7 +13,6 @@ use File::Slurp;

# And finally our modules
use STD;
use DumpMatch;

=head1 NAME
Expand All @@ -39,7 +38,7 @@ STD_syntax_highlight - Highlights Perl 6 source code using STD.pm
=cut

my ($clean_html,$help) = (0,0);
my ($full_html,$redspans_html) = (0,0,'-');
my ($full_html,$redspans_html) = (0,'-');
my ($file, $parser, $src_text);

# These are needed for redspans
Expand Down Expand Up @@ -94,7 +93,7 @@ HELP
$parser = STD->parsefile($file,$what);

# and finally print out the html code
highlight_match($what=>$parser,{});
highlight_match();
}

=item write_html_file
Expand All @@ -120,14 +119,8 @@ subroutine after traversing the STD parse tree using
DumpMatch.pm C<traverse_match>.
=cut
sub highlight_match {
my $name = shift;
my $r = shift;
my $opt = shift || {};
my $events = [];

if($full_html) {
traverse_match($r,$name,0,$events,$opt);
my $html = highlight_perl6_full($r,$::ORIG,$events,$opt);
my $html = highlight_perl6_full();
write_html_file $full_html, $html;
}
if($redspans_html) {
Expand All @@ -142,15 +135,24 @@ Generates the Perl6 highlighted HTML string for STD parse tree provided.
The resources can be inlined (by default) or externalized (--clean-html).
=cut
sub highlight_perl6_full {
my ($r,$orig,$events,$opt,$file) = @ARG,
my $str = "";
my $at = 0;

# slurp libraries and javascript to inline them
my ($JQUERY_JS,$JS,$CSS) = (
'jquery-1.2.6.pack.js',
'STD_syntax_highlight.js',
'STD_syntax_highlight.css');
my %colors = ();
my $line;
open CSS_FILE, $CSS
or die "Could not open $CSS: $OS_ERROR\n";
while($line = <CSS_FILE>) {
if($line =~ /^\s*\.(\w+)\s*{\s*color\s*:\s*(\w+)/) {
$colors{$1} = $2;
}
}
close CSS_FILE;

my $jquery_js = qq{<script type="text/javascript" src="../$JQUERY_JS"></script>};
my $js = qq{<script type="text/javascript" src="../$JS"></script>};
my $css = qq{<link href="../$CSS" rel="stylesheet" type="text/css">};
Expand Down Expand Up @@ -188,32 +190,50 @@ sub highlight_perl6_full {
<pre>
HTML

my $curr_rule = q{};
for (sort {$a->[0] <=> $b->[0] or $a->[4] <=> $b->[4]} @{$events}) {
my $text = substr($orig,$at,$_->[0]-$at);
my $esc_text .= escape_html($text);
$at = $_->[0];

if($curr_rule eq 'identifier') {
if($parser->is_type($text)) {
$str .= qq{<span class="_type">$esc_text</span>};
} elsif($parser->is_routine($text)) {
$str .= qq{<span class="_routine">$esc_text</span>};
my ($last_tree,$buffer) = ("","");
for my $i (0 .. @loc-1) {
next unless defined $loc[$i];
my $c = substr($src_text,$i,1);
my $tree = "@{$loc[$i]}";

if($tree ne $last_tree) {
$str .= qq{<span id="tree_$i" style="display:none;">$last_tree</span>};
my $rule;
my $rule_to_color = 0;
$buffer = escape_html($buffer);
my @rules = ();
@rules = reverse(split / /,$last_tree) if $last_tree ne '';
for $rule (@rules) {
if($rule eq 'unv') {
$rule_to_color = '_comment';
last;
} elsif($colors{$rule} && $buffer ne '') {
$rule_to_color = $rule;
last;
}
}
if($rule_to_color) {
if($last_tree =~ /identifier/) {
if($parser->is_type($buffer)) {
$str .= qq{<span id="node_$i" class="_type">$buffer</span>};
} elsif($parser->is_routine($buffer)) {
$str .= qq{<span id="node_$i" class="_routine">$buffer</span>};
} else {
$str .= qq{<span id="node_$i" class="$rule_to_color">$buffer</span>};
}
} else {
$str .= qq{<span id="node_$i" class="$rule_to_color">$buffer</span>};
}
} else {
$str .= $esc_text;
$str .= qq{<span id="node_$i">$buffer</span>};
}
$buffer = $c;
} else {
$str .= $esc_text;
}

if ($_->[1] eq 'from') {
$curr_rule = $_->[2];
$str .= '<span class="'.$curr_rule.'">';
} elsif ($_->[1] eq 'to') {
$str .= '</span>';
$buffer .= $c;
}
$last_tree = $tree;
}

$str .= <<"HTML";
</pre>
</body>
Expand Down Expand Up @@ -268,7 +288,7 @@ HTML

my ($last_tree,$buffer) = ("","");
for my $i (0 .. @loc-1) {
say("Undefined $i"),next unless defined $loc[$i];
next unless defined $loc[$i];
my $c = substr($src_text,$i,1);
my $tree = "@{$loc[$i]}";
if($tree ne $last_tree) {
Expand Down
28 changes: 6 additions & 22 deletions STD_syntax_highlight.js
@@ -1,41 +1,25 @@
$(document.body).ready(function() {
//find first span after pre and use it as top-level node
var topLevelRuleName = null;
var lastSelectedNode = null;
var keepResults = false;
var timeoutId = null;

$("pre > span").each(function(index,value) {
topLevelRuleName = this.className;
});

if(topLevelRuleName == "") {
alert("Assertion: Top Level node (pre > span) could not be found...");
return;
}
$("#parse_tree_output").html("Found " + $("span").size() + " node(s)");

function updateTree(node) {
if(lastSelectedNode) {
$(lastSelectedNode).css("border","");
$(lastSelectedNode).css("background-color","");
$(lastSelectedNode).css("border","");
$(lastSelectedNode).css("background-color","");
}
$(node).css("border","1px solid orange");
$(node).css("background-color","#FFF5DF");
lastSelectedNode = node;
var output = "";
var ident = "";
var rules = new Array();
var ruleName;
do {
var ruleName = node.className;
var r = ruleName;
rules.push(ruleName);
node = node.parentNode;
} while(ruleName != topLevelRuleName)
for(var i = rules.length - 1; i >= 0; i--) {
var r = rules[i];
output += ident + '<span class="' + r + '">' + r + '</span><br/>';
var rules = $("#" + node.id.replace("node","tree")).text().split(/ /);
for(var i = 0; i < rules.length; i++) {
var r = rules[i];
output += ident + '<span class="' + r + '">' + r + '</span><br/>';
ident += "&nbsp;";
}
$("#parse_tree_output").html(output);
Expand Down

0 comments on commit d7ee3e3

Please sign in to comment.