From db9049079b4a34b270283512941cfa19ac8d20aa Mon Sep 17 00:00:00 2001 From: Arne Georg Gleditsch Date: Sun, 17 Feb 2008 22:59:40 +0100 Subject: Add const as reserved word. --- lib/LXRng/Lang/C.pm | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/LXRng/Lang/C.pm b/lib/LXRng/Lang/C.pm index 928b450..60a571b 100644 --- a/lib/LXRng/Lang/C.pm +++ b/lib/LXRng/Lang/C.pm @@ -52,13 +52,12 @@ sub identifier_re { } my $_reserved ||= { map { $_ => 1 } - qw(asm auto break case char continue default do - double else enum extern float for fortran goto - if int long register return short signed sizeof - static struct switch typedef union unsigned - void volatile while - #define #else #endif #if #ifdef #ifndef #include - #undef)}; + qw(asm auto break case char const continue default + do double else enum extern float for fortran + goto if int long register return short signed + sizeof static struct switch typedef union + unsigned void volatile while #define #else + #endif #if #ifdef #ifndef #include #undef)}; sub reserved { return $_reserved; -- cgit v1.2.3 From 2d684362a152e9407ae143536fec2c33ffd48e6d Mon Sep 17 00:00:00 2001 From: Arne Georg Gleditsch Date: Sun, 17 Feb 2008 23:02:22 +0100 Subject: Better PDF generation, improved line split handling. --- lib/LXRng/Web.pm | 122 ++++++++++++++++++++++++++++------------------------- tmpl/print_pdf.tt2 | 3 +- tmpl/tex_funcs.tt2 | 39 +++++++++++++++++ 3 files changed, 104 insertions(+), 60 deletions(-) create mode 100644 tmpl/tex_funcs.tt2 diff --git a/lib/LXRng/Web.pm b/lib/LXRng/Web.pm index d7f9be4..6888b49 100644 --- a/lib/LXRng/Web.pm +++ b/lib/LXRng/Web.pm @@ -37,9 +37,6 @@ use File::Temp qw(tempdir tempfile); use File::Path qw(mkpath); use POSIX qw(waitpid); -use constant PDF_LINELEN => 95; -use constant PDF_CHARPTS => 6.6; - # Cache must be purged if this is changed. use constant FRAGMENT_SIZE => 250; @@ -592,25 +589,25 @@ sub generate_pdf { my $tempdir = tempdir(CLEANUP => 1); my %tspecials = ( - '$' => '\$', '*' => "\$\\ast\$", - '&' => '\&', '%' => '\%', - '#' => '\#', '_' => '\_', - '^' => '\^{}', '{' => '\{', - '}' => '\}', '|' => "\$|\$", - '[' => '{[}', ']' => '{]}', - "'" => "{'}", "\"" => "\\string\"", - '~' => '\~{}', '<' => "\$<\$", - '>' => "\$>\$", "\\" => "\$\\backslash\$", - '-' => '\dash{}', + '$' => '\$', '*' => "\$\\ast\$", + '&' => '\&', '%' => '\%', + '#' => '\#', '_' => '\_', + '^' => '\^{}', '{' => '\{', + '}' => '\}', '|' => "\$|\$", + '[' => '{[}', ']' => '{]}', + "'" => "{'}", "\"" => "\\string\"", + '~' => '\~{}', '<' => "\$<\$", + '>' => "\$>\$", "\\" => "\$\\backslash\$", + '-' => '\dash{}', # These are latin1-replacements, and interact badly with utf8... - "\242" => '?', "\244" => '?', - "\245" => '?', "\246" => '?', - "\252" => "\$\252\$", "\254" => "\$\254\$", - "\255" => "\\dash{}", "\260" => "\$\260\$", - "\261" => "\$\261\$", "\262" => "\$\262\$", - "\263" => "\$\263\$", "\265" => "\$\265\$", - "\271" => "\$\271\$", "\272" => "\$\272\$", - "\327" => "\$\327\$", "\367" => "\$\367\$", + "\242" => '?', "\244" => '?', + "\245" => '?', "\246" => '?', + "\252" => "\$\252\$", "\254" => "\$\254\$", + "\255" => "\\dash{}", "\260" => "\$\260\$", + "\261" => "\$\261\$", "\262" => "\$\262\$", + "\263" => "\$\263\$", "\265" => "\$\265\$", + "\271" => "\$\271\$", "\272" => "\$\272\$", + "\327" => "\$\327\$", "\367" => "\$\367\$", ); my $tspecials = join('', map { quotemeta($_) } keys(%tspecials)); @@ -630,7 +627,7 @@ sub generate_pdf { my $resre; if (%$res) { $resre = '(?:(?<=[\s\W])|^)('. - join('|', map { my $c = $_; $c =~ s/\#/\\\#/g; quotemeta($c) } + join('|', map { "\Q$_\E" } sort { length($b) <=> length($a) } keys %$res).')(?=$|[\s\W])'; } @@ -639,10 +636,12 @@ sub generate_pdf { my $row = 1; my $col = 0; my $line = '\\lxrln{1}'; + my %ptabs = (); + my %ntabs = (); while (1) { my ($btype, $frag) = $parse->nextfrag; - + last unless defined $frag; $btype ||= 'code'; @@ -655,6 +654,8 @@ sub generate_pdf { if ($part eq "\n") { push(@lines, $line); + %ptabs = %ntabs; + %ntabs = (); $col = 0; $row++; @@ -667,56 +668,61 @@ sub generate_pdf { next; } - if ($part =~ /^(.*? +)(.*)/) { - unshift(@parts, $2); + if ($part =~ /^(.*?)( +)(.*)/) { + unshift(@parts, $3); $part = $1; - $align = 1; - } - - $col += length($part); - - if ($col > PDF_LINELEN) { - unshift(@parts, - substr($part, PDF_LINELEN - $col, length($part), '')); - if ($part =~ s/([^\s_,\(\)\{\}\/\=\-\+\*\<\>\[\]\.]+)$//) { - if (length($1) < 20) { - unshift(@parts, $1); - } - else { - $part .= $1; - } + if (length($2) > 2 or $ptabs{$col + length($1) + length($2)}) { + $align = 1; + $col += length($2); + $ntabs{$col + length($part)} = 1; + } + else { + $part .= $2; } - $align = 0; - $cont = 1; } + $col += length($part); - $part =~ s(([$tspecials\0-\010\013\014\016-\037\200-\240])) - (exists $tspecials{$1} ? $tspecials{$1} : '?')ge; - if ($btype eq 'code') { - $part =~ s/$resre/\\textbf{$1}/g if $resre; + $part =~ s/$resre/\\bf{}\\sffamily{}$1\\sf{}/g if $resre; } elsif ($btype eq 'include') { - $part =~ s/$resre/\\textbf{$1}/ if $resre; + # This is a bit of a special treatment for C... + $part =~ s((<[^>]*>|\"[^\"]*\")|$resre) + ($1 ? "$1" : "\\bf{}\\sffamily{}$2\\sf{}")ge if $resre; } elsif ($btype eq 'comment') { - $part = '\textit{'.$part.'}'; + $part = '\\em{}'.$part.'\\sf{}'; } elsif ($btype eq 'string') { - $part = '\texttt{'.$part.'}'; + $part = '\\tt{}'.$part.'\\sf{}'; } - # Common fixed-width "ascii-art" characters. - $part =~ s/(\$\\ast\$|=)/'\\makebox['.PDF_CHARPTS."pt][c]{$1}"/ge; + $part =~ s{(\\(?:sf|bf|em|tt|sffamily)\{\})| + ([*=])| + ([ ]+)| + ([$tspecials\0-\010\013\014\016-\037\200-\240])| + ([[:alnum:]]+|.)} + { + if (defined $1) { + $1; + } + elsif (defined $2) { + "\\lxgr{".(exists $tspecials{$2} ? $tspecials{$2} : $2)."}"; + } + elsif (defined $3) { + "\\lxws{$3}"; + } + elsif (defined $4) { + "\\lxlt{".(exists $tspecials{$4} ? $tspecials{$4} : '?')."}"; + } + else { + "\\lxlt{$5}"; + } + }gex; + $line .= $part; if ($align) { - $line = '\\makebox['.int($col * PDF_CHARPTS). - 'pt][l]{'.$line.'}'; - } - if ($cont) { - push(@lines, "$line\\raisebox{-2pt}{\\ArrowBoldRightStrobe}"); - $line = '\\raisebox{-2pt}{\\ArrowBoldDownRight} '; - $col = 3; + $line .= "\\lxalign{$col}"; } } } diff --git a/tmpl/print_pdf.tt2 b/tmpl/print_pdf.tt2 index 0577159..29aca7c 100644 --- a/tmpl/print_pdf.tt2 +++ b/tmpl/print_pdf.tt2 @@ -10,6 +10,7 @@ \usepackage{fancyhdr} \usepackage{bbding} \pagestyle{fancy} +[% INCLUDE tex_funcs.tt2 %] \begin{document} \setlength\baselineskip{7pt} \setlength\parskip{.3\baselineskip} @@ -17,8 +18,6 @@ \renewcommand{\headrulewidth}{0pt} \fancyhead{} \fancyfoot{} -\def\dash{\raise2.1pt\hbox{\rule{5pt}{0.3pt}}\hspace{1pt}} -\newcommand\lxrln[1]{\tiny\hspace*{-4em}\makebox[4em][r]{#1\hspace{1.5ex}}\small} \sffamily\small %% I kinda like the proportional fonts, but expect they won't be %% everyone's cup of tea. Uncomment next line to use monospace font. diff --git a/tmpl/tex_funcs.tt2 b/tmpl/tex_funcs.tt2 new file mode 100644 index 0000000..36fee92 --- /dev/null +++ b/tmpl/tex_funcs.tt2 @@ -0,0 +1,39 @@ +% -*- latex -*- + +% LXRng formatting functions +\makeatletter +\newbox\lx@box +\newdimen\lx@charwd\advance\lx@charwd 6.4pt +\newdimen\lx@wrapped +\renewcommand\\{\newline\advance\lx@used-\lx@used\advance\lx@wrapped-\lx@wrapped} +\newdimen\lx@used +\newdimen\lx@linewd\advance\lx@linewd\textwidth\advance\lx@linewd-1em + +% Words +\def\lxlt#1{\setbox\lx@box\hbox{#1}% +\advance\lx@used\wd\lx@box% +\ifdim\lx@used>\lx@linewd% +\hspace*{-\lx@used}\hspace{\wd\lx@box}\hspace*{\lx@linewd}% +\raisebox{-2pt}{\ArrowBoldRightStrobe}\\% +\raisebox{-2pt}{\ArrowBoldDownRight} % +\advance\lx@wrapped\lx@linewd +\advance\lx@used-\lx@used% +\advance\lx@used 3\lx@charwd% +\advance\lx@used\wd\lx@box% +\else\fi% +\usebox\lx@box} + +% Alignment +\def\lxalign#1{\hspace*{#1\lx@charwd}\hspace*{-\lx@wrapped}\hspace*{-\lx@used}% +\advance\lx@used-\lx@used\advance\lx@used #1\lx@charwd} + +% Fixed-width chars +\def\lxgr#1{\lxlt{\makebox[\lx@charwd][l]{#1}}} + +% Whitespace +\def\lxws#1{\setbox\lx@box\hbox{#1}\hspace*{\wd\lx@box}\advance\lx@used\wd\lx@box} + +\newcommand\lxrln[1]{\tiny\hspace*{-4em}\makebox[4em][r]{#1\hspace{1.5ex}}\small} +\def\dash{\raise2.1pt\hbox{\rule{5pt}{0.3pt}}\hspace{1pt}} + +\makeatother -- cgit v1.2.3 From 92b25824ab4f0d0ba0cc4df195396537eb5f5efb Mon Sep 17 00:00:00 2001 From: Arne Georg Gleditsch Date: Sun, 17 Feb 2008 23:03:30 +0100 Subject: Cruft cleanup, whitespace changes. --- lib/LXRng/Web.pm | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/lib/LXRng/Web.pm b/lib/LXRng/Web.pm index 6888b49..126bf9b 100644 --- a/lib/LXRng/Web.pm +++ b/lib/LXRng/Web.pm @@ -83,7 +83,7 @@ sub print_markedup_file { my $fline = $context->param('line'); $focus = $fline < 100 if defined($fline); - + my $shaid = sha1_hex(join("\0", $node->name, $node->revision, $context->release)); my $cfile; @@ -134,7 +134,7 @@ sub print_markedup_file { "\" id=\"$shaid/0\">"); while (1) { my @frags = map { split(/(?<=\n)/, $_) } - $markup->markupfile($subst, $parse); + $markup->markupfile($subst, $parse); last unless @frags; foreach my $f (@frags) { print($f) if $focus; @@ -174,7 +174,7 @@ sub print_tree_list { $template->process('tree_list.tt2', {'context' => $context, 'base_url' => $base}) - or die $template->error(); + or die $template->error(); } sub print_release_list { @@ -182,7 +182,7 @@ sub print_release_list { $template->process('release_select.tt2', {'context' => $context}) - or die $template->error(); + or die $template->error(); } sub source { @@ -322,29 +322,8 @@ sub source { # 0.18, this seems to truncate the stream. Not strictly needed # for CGI, reexamine when adapting to mod_perl. ## binmode(\*STDOUT, ":pop") if $gzip; - } -#sub ident { -# my ($self) = @_; - -# my $index = $self->context->config->{'index'}; -# my $view = LXRng::View->new('context' => $self->context);; - -# my $ident = $self->context->value('ident'); -# my $target = $self->context->value('navtarget'); -# $target ||= 'source'; - -# my $rel_id = $index->release_id($self->tree, $self->context->value('v')); -# my ($symname, $symid, $ident, $refs) = -# $index->get_identifier_info($ident, $rel_id); - -# $$ident[1] = $LXRng::Lang::deftypes{$$ident[1]}; -# $$ident[5] &&= $LXRng::Lang::deftypes{$$ident[5]}; - -# return $view->identifier_info($symname, $symid, $ident, $refs, $target); -#} - # Perform various search operations. Return results as html suitable # both as a response to an ajax request and inclusion in a more @@ -399,8 +378,8 @@ sub search { $_ } sort { $LXRng::Lang::defweight{$$b[1]} cmp $LXRng::Lang::defweight{$$a[1]} || - $$a[2] cmp $$b[2] || - $$a[3] <=> $$b[3] } + $$a[2] cmp $$b[2] || + $$a[3] <=> $$b[3] } @$result); $template_args{'code_res'} = {'query' => $find, 'idents' => \@cooked}; @@ -566,7 +545,7 @@ sub handle_preferences { my $nav = 'is_replace'; $nav = 'is_'.$context->prefs->{'navmethod'} if $context->prefs and $context->prefs->{'navmethod'} ne ''; - + my $ret = $context->base_url(); $ret =~ s,[+]prefs/?,,; $ret .= $query->param('return') if $query->param('return'); -- cgit v1.2.3 From dd670fda9351186a1bf51de2a3b8192a9cda301c Mon Sep 17 00:00:00 2001 From: Arne Georg Gleditsch Date: Sun, 17 Feb 2008 23:04:29 +0100 Subject: Sentinel file to indicate cache entry complete. --- lib/LXRng/Web.pm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/LXRng/Web.pm b/lib/LXRng/Web.pm index 126bf9b..78380c8 100644 --- a/lib/LXRng/Web.pm +++ b/lib/LXRng/Web.pm @@ -91,7 +91,7 @@ sub print_markedup_file { $cfile = $context->config->{'cache'}.'/'.$shaid if exists $context->config->{'cache'}; - if ($cfile and -d $cfile) { + if ($cfile and -e "$cfile/.complete") { print("
");
 	    while (-r "$cfile/$line") {
 		print("
\n"); - close($cache) if $cache; + if ($cache) { + close($cache); + open($cache, '>', "$cfile/.complete"); + close($cache); + } } return $shaid; } -- cgit v1.2.3 From 32aa8b091f439043ec2a4c7807366cb26eb9de19 Mon Sep 17 00:00:00 2001 From: Arne Georg Gleditsch Date: Sun, 17 Feb 2008 23:05:18 +0100 Subject: Improved error handling. --- lib/LXRng/Context.pm | 2 +- lib/LXRng/Web.pm | 50 +++++++++++++++++++++++++++++++++++++++++++++----- tmpl/bare_error.tt2 | 20 ++++++++++++++++++++ tmpl/error.tt2 | 3 +++ tmpl/header.tt2 | 24 ++++++++++++------------ tmpl/search_result.tt2 | 4 ++++ 6 files changed, 85 insertions(+), 18 deletions(-) create mode 100644 tmpl/bare_error.tt2 create mode 100644 tmpl/error.tt2 diff --git a/lib/LXRng/Context.pm b/lib/LXRng/Context.pm index 6909c58..db486bf 100644 --- a/lib/LXRng/Context.pm +++ b/lib/LXRng/Context.pm @@ -82,7 +82,7 @@ sub new { if ($$self{'tree'} and $$self{'tree'} !~ /^[+]/) { my $tree = $$self{'tree'}; - die("No config for tree $tree") + return $self unless exists($$config{$tree}); $$self{'config'} = $$config{$tree}; diff --git a/lib/LXRng/Web.pm b/lib/LXRng/Web.pm index 78380c8..e616170 100644 --- a/lib/LXRng/Web.pm +++ b/lib/LXRng/Web.pm @@ -170,6 +170,28 @@ sub print_markedup_file { } } +sub print_error { + my ($context, $template, $query, $error) = @_; + + my $tmpl; + if ($context->config and $context->config->{'repository'}) { + $tmpl = 'error.tt2'; + } + else { + $tmpl = 'bare_error.tt2'; + } + + print($query->header(-type => 'text/html', + -charset => 'utf-8')); + + my $base = $context->base_url(); + $template->process($tmpl, + {'context' => $context, + 'base_url' => $base, + 'error' => $error}) + or die $template->error(); +} + sub print_tree_list { my ($context, $template) = @_; @@ -243,10 +265,18 @@ sub source { my $ver = $context->release; my $rep = $context->config->{'repository'}; - die "No tree given" unless $rep; + unless ($rep) { + print_error($context, $template, $query, + "No/unknown tree indicated"); + return; + } my $node = $rep->node($context->path, $ver); - die "Node not found: ".$context->path." ($ver)" unless $node; + unless ($node) { + print_error($context, $template, $query, + "Node not found: ".$context->path." ($ver)"); + return; + } my $gzip = do_compress_response($query); @@ -404,13 +434,13 @@ sub search { my $rep = $context->config->{'repository'}; my @args = grep { $rep->node($_, $context->release) - } split(/\|/, $find); + } split(/\|/, $find); $template_args{'ambig_res'} = {'query' => $find, 'files' => \@args,} } } else { - die "No query string given"; + $template_args{'error'} = 'No query string given'; } my $html = ''; $template_args{'tree'} = $context->tree; @@ -473,6 +503,10 @@ sub handle_ajax_request { binmode(\*STDOUT, ":gzip") if $gzip; if ($context->param('fname') eq 'pjx_load_file') { + unless ($context->config and $context->config->{'repository'}) { + print('
No/unknown tree indicated.
'); + return; + } my $rep = $context->config->{'repository'}; my $node = $rep->node($context->param('file'), $context->release); print_markedup_file($context, $template, $node); @@ -773,8 +807,14 @@ sub generate_pdf { sub handle { my ($self, $query) = @_; - my $context = LXRng::Context->new('query' => $query); my $template = Template->new({'INCLUDE_PATH' => $LXRng::ROOT.'/tmpl/'}); + my $context = LXRng::Context->new('query' => $query); + + unless ($context->config) { + print_error($context, $template, $query, + "No/unknown tree indicated"); + return; + } if ($context->param('fname')) { handle_ajax_request($query, $context, $template); diff --git a/tmpl/bare_error.tt2 b/tmpl/bare_error.tt2 new file mode 100644 index 0000000..03f6aeb --- /dev/null +++ b/tmpl/bare_error.tt2 @@ -0,0 +1,20 @@ + + + + + LXR Error + + + + + +
[% error | html %]
+ + diff --git a/tmpl/error.tt2 b/tmpl/error.tt2 new file mode 100644 index 0000000..b8996ac --- /dev/null +++ b/tmpl/error.tt2 @@ -0,0 +1,3 @@ +[% INCLUDE header.tt2 %] +
[% error | html %]
+[% INCLUDE footer.tt2 %] diff --git a/tmpl/header.tt2 b/tmpl/header.tt2 index 2932823..106f3bd 100644 --- a/tmpl/header.tt2 +++ b/tmpl/header.tt2 @@ -10,19 +10,19 @@ var startup_tree = location.hash.match(/^#([^/+]+)([+][^/]*|)/); if (startup_tree) { - var base = '[% base_url %]'; + var base = '[% base_url | html %]'; base = base.replace(/\/ajax[+][*]/, '/' + startup_tree[1] + '+*'); document.write(''); } else { - document.write(''); + document.write(''); document.write(''); use_ajax_navigation = 0; } [% END %] [% IF !is_ajax %] - + [% END %] @@ -32,8 +32,8 @@ - LXR [% context.tree -%]/ - [%- FOREACH elem = context.path_elements %][% elem.node %][% END %] + <title>LXR [% context.tree | html -%]/ + [%- FOREACH elem = context.path_elements %][% elem.node | html %][% END %] @@ -47,15 +47,15 @@ - [% context.tree %]/[% + [% context.tree | html%]/[% FOREACH elem = context.path_elements - %][% elem.node %][% + %][% elem.node | html %][% END %] History -