From: Sergey Matveev Date: Tue, 10 Jun 2025 12:50:05 +0000 (+0300) Subject: LaTeX output X-Git-Tag: v1.0.0^0 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=275c4d47c699a8fd36828254a57424887a26e614;p=swg.git LaTeX output --- diff --git a/USAGE b/USAGE index 117daf0..d700cab 100644 --- a/USAGE +++ b/USAGE @@ -10,6 +10,8 @@ Generates Graphviz'es dot graph with internal links relationships. * swg info >out.info Generates GNU Info output. +* swg latex >out.tex + Generates LaTeX output. If directory does not have "index" file, then it will be autogenerated with all files in the directory (category) and all backlinks to it. diff --git a/swg b/swg index 8d54bed..34fc2c9 100755 --- a/swg +++ b/swg @@ -21,8 +21,9 @@ Usage: $0 backs PAGE -- show backlinks to the PAGE $0 cats [PREFIX] -- show categories $0 htmls DIR -- render HTMLs in DIR - $0 info >out.info $0 dot | dot -Tpng >relations.png + $0 info >out.info + $0 latex >out.tex For debugging: $0 files -- list files that will be processed @@ -438,6 +439,71 @@ sub genInfo { return; } +sub genLaTeXSection { + my $page = shift; + my $buf = shift; + print <<"END_LATEX" +\\addcontentsline{toc}{section}{$page} +\\section*{$page} +\\begin{verbatim} +END_LATEX +; + my $fh; + if (defined $buf) { + open $fh, q{<:encoding(UTF-8)}, \$buf or croak "$!"; + } else { + open $fh, q{<:encoding(UTF-8)}, $page or croak "$!"; + } + my $wasClosed = 0; + while (<$fh>) { + chop; + if (/${CR}$/) { + chop; + /^\s*(.*)$/; + my $cmd = $1; + if (($cmd =~ /^#/) or ($cmd =~ /^do-backs/)) { + next; + } + if ($cmd =~ /^=> (\S+)\s?(.*)$/) { + my $url = $1; + my $t = ($2 eq q{}) ? $1 : $2; + if (not $wasClosed) { + print "\\end{verbatim}\n"; + } + print "\$\\Rightarrow\$ \\href{$url}{$t} \\newline\n"; + $wasClosed = 1; + next; + } elsif ($cmd =~ /^img (\S+)\s?(.*)$/) { + if (not $wasClosed) { + print "\\end{verbatim}\n"; + } + print "\\includegraphics{$1} \\newline\n"; + next; + } + } + if ($wasClosed) { + $wasClosed = 0; + print "\\begin{verbatim}\n"; + } + if (/^(.*)\\end\{verbatim\}(.*)$/) { + print "$1\\end{verbatim}\\verb|\\end{verbatim}|\\begin{verbatim}$2"; + next; + } + print "$_\n"; + } + my @backs = sort keys %{$Backs{noindex $page}}; + if ($#backs != -1) { + print "\nBacklinks:\n"; + foreach (@backs) { + print "[$_]\n"; + } + } + if (not $wasClosed) { + print "\\end{verbatim}\n"; + } + return; +} + sub genInfoIndex { my $page = shift; my $pth; @@ -586,6 +652,42 @@ if ($ARGV[0] eq q{files}) { (nodename noindex $page) . ". (line 0)\n"; } print "${sep}Local Variables:\ncoding: utf-8\nEnd:\n"; +} elsif ($ARGV[0] eq q{latex}) { + print <<"END_LATEX" +\\documentclass[a4paper,10pt]{report} +\\usepackage{iftex} +\\ifxetex + \\usepackage{fontspec} + \\setmainfont{CMU Serif} + \\setmonofont{Go Mono} +\\else + \\usepackage[T2A]{fontenc} + \\usepackage[utf8]{inputenc} +\\fi +\\usepackage[pdfa,colorlinks=true,final]{hyperref} +\\usepackage{a4wide} +\\usepackage{graphicx} +\\begin{document} +\\small +END_LATEX +; + if (exists $Mtimes{q{index}}) { + genLaTeXSection q{index}; + delete $Mtimes{q{index}}; + delete $CatFiles{q{/}}; + } else { + genLaTeXSection "/", genIndex2Buf q{/}; + delete $CatFiles{q{/}}; + } + foreach my $cat (keys %CatFiles) { + next if (exists $Mtimes{"${cat}index"}); + genLaTeXSection $cat, genIndex2Buf $cat; + } + foreach my $page (keys %Mtimes) { + genLaTeXSection $page; + } + print "\\tableofcontents\n"; + print "\\end{document}\n"; } elsif ($ARGV[0] eq q{dot}) { print "digraph d {\n"; print "rankdir=LR\n";