New file .my-git-push.
[grutatxt.git] / grutatxt
blob0f5696ab2af1e4fd895c92904db7b974462ec071
1 #!/usr/bin/perl
3 use lib '.';
6 # grutatxt - Text to HTML (and other formats) converter
7 # http://triptico.com/software/grutatxt.html
9 # Copyright (C) 2000/2011 Angel Ortega <angel@triptico.com>
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 # http://www.triptico.com
28 use Getopt::Long;
29 use locale;
30 use Grutatxt;
32 $VERSION = $Grutatxt::VERSION . ':1';
34 # input file
35 $input_file = '-';
37 # output file
38 $output_file = '>-';
40 # CSS information
41 $css = '';
42 $embed_css = 0;
44 # page title
45 $title = '';
47 # offset for the h? headers
48 $header_offset = 0;
50 # default mode
51 $mode = 'HTML';
53 # use real dl
54 $dl_as_dl = 0;
56 # troff table type
57 $table_type = 'allbox';
59 # abstract line number
60 $abstract = 0;
62 # man page section
63 $man_section = 1;
65 # default tab size in LaTeX mode
66 $tabsize = 8;
68 # avoid time signature
69 $no_time_sig = 0;
71 # disable pure verbatim mode
72 $no_pure_verbatim = 0;
74 # enable TOC
75 $toc = 0;
77 #####################################################################
79 # parse options
80 if (!GetOptions('i|input=s' => \$input_file,
81 'o|output=s' => \$output_file,
82 'c|css=s' => \$css,
83 'e|embed-css' => \$embed_css,
84 't|title=s' => \$title,
85 'f|header-offset=s' => \$header_offset,
86 'b|table-headers' => \$table_headers,
87 'ct|center-tables' => \$center_tables,
88 'xt|expand-tables' => \$expand_tables,
89 'sp|strip-parens' => \$strip_parens,
90 'ts|tabsize=s' => \$tabsize,
91 'nb|no-body' => \$no_body,
92 'v|version' => \$version,
93 'h|help' => \$usage,
94 'm|mode=s' => \$mode,
95 's|man-section=s' => \$man_section,
96 'docclass=s' => \$latex_docclass,
97 'papersize=s' => \$papersize,
98 'encoding=s' => \$encoding,
99 'dl' => \$dl_as_dl,
100 'no-time-sig' => \$no_time_sig,
101 'no-pure-verbatim' => \$no_pure_verbatim,
102 'toc' => \$toc
103 ) or $usage) {
104 usage();
107 if ($version) {
108 print "$VERSION\n"; exit(0);
111 open I, $input_file or die "Can't open $input_file: $!";
112 open O, ">$output_file" or die "Can't create $output_file: $!";
114 # if utf8 encoding is wanted, set the filehandles as utf8
115 # so that regular expressions match all characters
116 # (this is crap)
117 if (defined($encoding) && $encoding =~ /^utf-?8/i) {
118 binmode(I, ":utf8");
119 binmode(O, ":utf8");
122 $content = join('',<I>);
123 close I;
125 $content_title = '';
127 # make tab to space conversion only in LaTeX mode
128 $tabsize = 0 unless $mode =~ /^latex$/i;
130 $grutatxt = new Grutatxt(
131 'mode' => $mode,
132 'header-offset' => $header_offset,
133 'table-headers' => $table_headers,
134 'center-tables' => $center_tables,
135 'expand-tables' => $expand_tables,
136 'strip-parens' => $strip_parens,
137 'strip-dollars' => $strip_dollars,
138 'tabsize' => $tabsize,
139 'dl-as-dl' => $dl_as_dl,
140 'table-type' => $table_type,
141 'title' => \$content_title,
142 'abstract' => \$abstract,
143 'page-name' => $title,
144 'section' => $man_section,
145 'docclass' => $latex_docclass,
146 'papersize' => $papersize,
147 'encoding' => $encoding,
148 'no-pure-verbatim' => $no_pure_verbatim,
149 'toc' => $toc
152 @result = $grutatxt->process($content);
154 $title = $content_title unless $title;
155 $no_body = 1 unless $mode =~ /^html$/i;
157 unless ($no_body) {
158 print O "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"\n";
159 print O " \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n";
160 print O "<html><head>\n";
161 print O "<meta http-equiv='Content-Type' content='text/html; charset=" .
162 ($encoding || 'iso-8859-1') . "'>\n";
163 print O "<title>$title</title>\n";
165 printf O "<!-- converted from text by grutatxt $VERSION on %s -->\n", scalar(localtime)
166 unless $no_time_sig;
168 if ($css) {
169 if ($embed_css) {
170 if (open C, $css) {
171 my ($c) = join('', <C>);
172 close C;
174 print O "<style type='text/css'>\n";
175 print O $c . "\n";
176 print O "</style>\n";
178 else {
179 die "Can't open '$css' CSS file.";
182 else {
183 print O "<link rel=StyleSheet href='$css' type='text/css'>";
187 print O "</head><body>\n";
190 foreach my $l (@result) {
191 print O "$l\n";
194 print O "</body></html>\n" unless $no_body;
196 close O;
198 exit(0);
201 sub usage
203 print "grutatxt $VERSION - Grutatxt format processor\n";
204 print "Copyright (C) 2000/2011 Angel Ortega <angel\@triptico.com>\n";
205 print "This software is covered by the GPL license. NO WARRANTY.\n\n";
207 print "Usage:\n";
208 print "\n";
209 print "grutatxt [options] < input_text_file > output_html_file\n";
210 print "\n";
211 print "Global options:\n\n";
212 print " -i|--input=FILE Input file (STDIN)\n";
213 print " -o|--output=FILE Output file (STDOUT)\n";
214 print " -t|--title=TITLE Document title (if unset,\n";
215 print " level 1 heading is used)\n";
216 print " -sp|--strip-parens Strip parentheses in function\n";
217 print " names (shown monospaced anyway)\n";
218 print " -sd|--strip-dollars Strip leading \$ in variable\n";
219 print " names (shown monospaced anyway)\n";
220 print " -m|--mode=[HTML|troff|man|latex|rtf]\n";
221 print " Output mode: HTML, troff, man, LaTEX or RTF\n";
222 print " (default: HTML)\n";
223 print " --no-time-sig Avoid time signature in HTML comment\n";
224 print " --no-pure-verbatim Disable pure verbatim mode\n";
225 print " --toc Add a table of contents after abstract\n\n";
226 print "HTML options:\n\n";
227 print " -c|--css=CSS_URL_OR_FILE CSS URL (or file if using --embed-css)\n";
228 print " -e|--embed-css Embed CSS instead of linking to it\n";
229 print " -f|--header-offset=NUMBER Offset to add to <h1>,\n";
230 print " <h2>... headers (default 0)\n";
231 print " -b|--table-headers Use <th> instead of <td> in\n";
232 print " the first row of each table\n";
233 print " -ct|--center-tables Centers the tables\n";
234 print " -xt|--expand-tables Expands the tables (width=100\%)\n";
235 print " -nb|-no-body Don't generate <html><body>...\n";
236 print " </body></html> enclosing.\n";
237 print " --encoding=ENCODING Character encoding (default: iso-8859-1)\n";
238 print " -dl Use real <dl>, <dd> and <dt>\n";
239 print " instead of tables in definition lists.\n";
240 print "troff options:\n\n";
241 print " --table-type=TYPE Table type. Possible values:\n";
242 print " box, allbox, doublebox (default allbox)\n";
243 print "man options:\n\n";
244 print " -s|--man-section=SECTION Man page section (default: 1)\n\n";
245 print "LaTeX options:\n\n";
246 print " --docclass=CLASS Document class (default: report)\n";
247 print " --papersize=SIZE Paper size (default: a4paper)\n";
248 print " --encoding=ENCODING Character encoding (default: latin1)\n";
249 print " -ts|--tabsize=NUMBER Tab size for tab to space conversions in\n";
250 print " LaTeX verbatim environment (default: 8)\n";
252 exit(1);