USAGE | 2 ++ swg | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++- diff --git a/USAGE b/USAGE index f2b36499090b5424c6c44fad8b6de5f6049af397fc8fa6ea7c44607a9b81005f..d0ae5afa85faa320f63a1a039681c3dc66075f4d7c18c568e597c91aa3c083c5 100644 --- a/USAGE +++ b/USAGE @@ -10,6 +10,8 @@ * swg dot | dot -Tpng >relations.png 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 ae5180bf562c75fd2539adc77288d43db99db25f3962b5c2a268d120746a34ee..8d68ba5c2e44d24b6c02eccfc8e1be550a974395376e256e6134ed5f15cf7507 100755 --- a/swg +++ b/swg @@ -21,8 +21,9 @@ $0 links PAGE -- show links on the PAGE $0 backs PAGE -- show backlinks to the PAGE $0 cats [PREFIX] -- show categories $0 htmls DIR -- render HTMLs in DIR + $0 dot | dot -Tpng >relations.png $0 info >out.info - $0 dot | dot -Tpng >relations.png + $0 latex >out.tex For debugging: $0 files -- list files that will be processed @@ -438,6 +439,71 @@ } 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 @@ print q{* } . (nodename noindex $page) . q{: } . (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";