-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobo-syntax-highlight-in-html.pl
executable file
·149 lines (138 loc) · 2.56 KB
/
obo-syntax-highlight-in-html.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/perl -w
use strict;
use Data::Stag qw(:all);
my @hdrs = ();
my @nodedivs = ();
my $base = "http://purl.org/obo/html";
my $title = "OBO";
while ($ARGV[0] =~ /^\-/) {
my $opt = shift @ARGV;
if ($opt eq '-t' || $opt eq '--title') {
$title = shift @ARGV;
}
else {
die $opt;
}
}
my $in_hdr = 1;
my %idh;
my @lines=();
while (<>) {
push(@lines,$_);
if (/^id: (.*)\n/) {
$idh{$1}= 1;
}
}
foreach (@lines) {
chomp;
if (/^\[/ && $in_hdr) {
$in_hdr = 0;
}
if ($in_hdr) {
push(@hdrs,$_);
}
if (/^(\[.*)/) {
push(@nodedivs,["<span class='stanza-open'>$1</span>"]);
}
my $node = $nodedivs[-1];
my @elts = ();
my $cmts = '';
if (/^(.*)(\!.*)/) {
$_ = $1;
$cmts = $2;
}
if (/^([\w\-]+):\s*(.*)/) {
my ($k,$v) = ($1,$2);
if ($k eq 'id') {
push(@elts,"<a name='$v'/>");
}
else {
$v =~ s/\"([^\"]*)\"/\"\<span class=\"quoted\"\>$1\<\/span\>\"/g;
$v =~ s/\[([^\]]*)\]/<span class=\"bracketed\"\>\[$1\]\<\/span\>/g;
#$v =~ s/(\w+:\S+)/\<a class=idref href=\'$1\'\>$1\<\/a\>/ge;
#$v =~ s/(\w+:\S+)/<a class=\"idref\" href=\"idlink($1)\"\>$1\<\/a\>/g;
$v =~ s/(http:.*)/http_href($1)/ge;
$v =~ s/(\w+:[\w\-]+)/idlink($1)/ge;
}
push(@elts,
"<span class='key'>$k</span>: ",
"<span class='value'>$v</span>",
);
}
else {
}
push(@elts,
"<span class='comment'>$cmts</span>");
if ($node) {
push(@$node, "<span>".join('',@elts)."</span>\n");
}
}
my $body = join("\n",map {"<div class='node'>".join('',@$_)."</div>"} @nodedivs);
print <<EOM
<html>
<head>
<title>$title</title>
<style type="text/css">
<!--
.comment
{
color: #f00;
font-style: italic;
}
.quoted
{
color: #800;
font-weight: bold;
font-style: italic;
}
.key
{
color: #008;
font-weight: bold;
}
.value
{
color: #000;
}
.idref
{
color: #488;
}
.bracketed
{
font-weight: bold;
}
.stanza-open
{
color: #882;
font-size: large;
}
-->
</style>
</head>
<body>
<pre>
$body
</pre>
</body>
</html>
EOM
;
exit 0;
sub idlink {
my $id = shift;
my $href = "#".$id;
if (!$idh{$id}) {
if ($id =~ /(\w+):/) {
$href="$base/$1.html#".$id;
}
else {
$href="$base#".$id;
}
}
return "<a class=\"idref\" href=\"$href\">$id</a>";
}
sub http_href {
my $url = shift;
return "<a class=\"idref\" href=\"$url\">$url</a>";
}