#!/usr/bin/env perl #------------------------------------------------------------------------------ # LawTeX - LaTeX for Law School # Copyright 2007-2010, Rohit Nafday. All rights reserved. # # This program is free software; redistribution and use in source and binary # forms, with or without modification, are permitted provided that the # following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # $Id: lawtex.pl 16 2017-10-14 20:08:56Z rohit $ #------------------------------------------------------------------------------ =pod =head1 NAME lawtex.pl - Convert LawTeX marked up plain text to TeX =head1 SYNOPSIS # convert markup to tex latex.pl notes.txt notes.tex =head1 DESCRIPTION This program is a utility designed to convert plain-text notes, written using basic LawTeX markup notation into TeX, which can later be combined in a larger LaTeX file to generate beautiful typeset notes. The syntax for LawTeX markup is similar, if not identical, to most Wiki markdown. This syntax is summarized in the LawTeX documentation. See: L =cut use strict; use warnings; use Getopt::Long; use IO::File; my ($txt, $tex, $content, @lines, %OPTS); #------------------------------------------------------------------------------ # Command Line Options #------------------------------------------------------------------------------ &GetOptions( 'help' => \$OPTS{help}, ); # print help and exit unless arguments are defined unless(!$OPTS{help} && defined($ARGV[0]) && -e $ARGV[0] && defined($ARGV[1])) { help(); exit; } #------------------------------------------------------------------------------ # Files #------------------------------------------------------------------------------ # input and output $txt = IO::File->new("< $ARGV[0]"); $tex = IO::File->new("> $ARGV[1]"); # pull in text file @lines = <$txt>; #------------------------------------------------------------------------------ # Transliterate Markdown Text to TeX #------------------------------------------------------------------------------ while(my $line = shift(@lines)) { next if($line =~ /^\s*$/); if($line =~ /^Date: (.*)$/ || $line =~ /^\>\> (.*)$/) { # date $content .= "\\vspace{10pt}\\begin{center} --- \\textsc{$1} --- \\end{center}\n"; } elsif($line =~ /^\s*\*\s/) { # section $line =~ s/\s*\*\s//g; $line =~ s/\n//; $content .= "\\section{" . build_inline_elements($line) . "}\n"; } elsif($line =~ /^\s*\*\*\s/) { # subsection $line =~ s/\s*\*\*\s//g; $line =~ s/\n//; $content .= "\\subsection{" . build_inline_elements($line) . "}\n"; } elsif($line =~ /^\s*\*\*\*\s/) { # subsubsection $line =~ s/\s*\*\*\*\s//g; $line =~ s/\n//; $content .= "\\subsubsection{" . build_inline_elements($line) . "}\n"; } elsif($line =~ /^\s*(\-+)/) { # itemized list unshift(@lines, $line); # put line back on top $content .= build_itemized_list($1); } elsif($line =~ /^\s*(\d)\./) { # numerical list unshift(@lines, $line); # put line back on top $content .= build_enumerated_list(); } elsif($line =~ /^\s*\#/) { # raw LaTeX code $line =~ s/^\s*\#//; $content .= $line =~ /\\begin\{table\}/ ? $line : build_inline_elements($line); } else { $content .= build_inline_elements($line); } } # print tex file and exit print $tex $content; exit; #------------------------------------------------------------------------------- # Subroutines #------------------------------------------------------------------------------- sub build_inline_elements { my ($line) = @_; $line =~ s/\"(.*?)\"/``$1''/g; # quotations $line =~ s/\[(.*?)\]/\\emph\{$1\}/g; # emphasis $line =~ s/\\S /\$\\S\$\~/g; $line =~ s/\\P/\$\\P\$/g; return $line; } sub build_enumerated_list { my $enumerate = "\\begin{enumerate}\n"; while(my $line = shift(@lines)) { if($line !~ /\s*\d+\./) { unshift(@lines, $line); last; } if($line =~ /^\s*\d+\.\s/) { $line =~ s/^\s*\d+\.\s//; $enumerate .= "\\item " . build_inline_elements($line); } elsif($line =~ /^\s*(\-+)/) { unshift(@lines, $line); # put line back on top $enumerate .= build_itemized_list($1); } } $enumerate .= "\\end{enumerate}\n"; return $enumerate; } sub build_itemized_list { my ($symbol) = @_; my $itemize = "\\begin{itemize}\n"; # escape the symbol for use in regexp $symbol =~ s/(\-)/\\$1/g; while(my $line = shift(@lines)) { if($line !~ /^\s*$symbol/) { unshift(@lines, $line); if($line =~ /^\s*(\d+)\.\s/) { $itemize .= build_enumerated_list(); } last; } if($line =~ /^\s*$symbol\s/) { $line =~ s/^\s*\-+/\\item /; $itemize .= build_inline_elements($line); } else { $line =~ /^\s*(.*?)\s/; unshift(@lines, $line); $itemize .= build_itemized_list($1); } } $itemize .= "\\end{itemize}\n"; return $itemize; } sub help { print < L =head1 COPYRIGHT Copyright 2007-2010, Rohit Nafday. All rights reserved. This is free software; you may distribute it under a BSD-style license. See L. =cut