]> Sergey Matveev's repositories - swg.git/commitdiff
LaTeX output v1.0.0
authorSergey Matveev <stargrave@stargrave.org>
Tue, 10 Jun 2025 12:50:05 +0000 (15:50 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Tue, 10 Jun 2025 12:50:05 +0000 (15:50 +0300)
USAGE
swg

diff --git a/USAGE b/USAGE
index 117daf06ab71186c8ada39478e41d6ed92db404f..d700cab99ce058173d58013dc7d9140c6f8d786d 100644 (file)
--- 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 8d54bed5341ef7f8f564513ab3aaefa25923a243..34fc2c90ac1faa1b9b15218ea7eab196b3621275 100755 (executable)
--- 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";