File: j_mkhtml

package info (click to toggle)
journal-dev 1-4
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 72 kB
  • ctags: 9
  • sloc: perl: 135; makefile: 50
file content (141 lines) | stat: -rwxr-xr-x 4,592 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#! /usr/bin/perl
#
# Last modification: Mon, 28 Oct 1996 08:56:04 +0200
#
# j_mkhtml  - Copyright (c)1996 by Fabrizio Polacco <fpolacco@debian.org>.
# All rights reserved.  This program is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.

# This script is intended to be used to build the debian package of the html
# on-line magazine Pluto Journal, but can be used to easily build also other
# online magazines.

# Usage: j_mkhtml <source-dir> <dest-dir>
# the script executes in the source dir where there are:
# "html.wrapper", "html.list", one "<file>.body" for each "<file>.html" listed
# in the "html.list" file.

# This script reads the file "html.list" and builds a list of values from its
# contents; then for each filename listed it processes the file "html.wrapper"
# copying the output to <dest-dir>/<filename>.html The process consists in
# executing special commands and substituting special variables.

# Special commands are:

#	@@!include <filename>@@	substitute the command string with the content
#				of <filename>
#	@@!include BODY @@	substitute the command string with the content
#				of current <@@filename@@>.body
#	@@!erase @@		don't copy this line to dest file (the line is
#				not processed and other commands will be ignored)

# The variables are:
#	@@filename@@	the name of the file in the list, without extension
#	@@prev-name@@	the filename of the previous row.
#	@@next-name@@	the filename of the next row.
#	@@index-name@@	the filename of the index file, usually "../index."
#	@@title@@	the string to be used in the <title> tag
#	@@heading@@	the string to appear in large font on top of the page
#	@@type@@	the type of the page, to appear on top and foot
#	@@alt@@		the string to be used within square brackets as 
#			ALT attribute in IMG links that points to this page
#	@@prev-alt@@	the ALT attribute string of the previous page
#	@@next-alt@@	the ALT attribute string of the next page
#	@@index-alt@@	the ALT attribute string of the index page

# the first file in the list is considered the INDEX row, a special case, whose
# contents are used as the "prev" values for the first file (the row after this
# one) and as the "next" for the last value.

# index file itself is considered being outside of this process, with directs
# links to the files in the dest-dir.
#

# needs require libwww-perl to compile package
use HTML::Entities %char2entity;

sub process_file
{
	local( *IN, $file, $remain ); ($IN) = @_;	# my() can't use filehandles
	open(IN, $IN) or die "Cannot open input file $IN\n";
	while ( <IN> )
	{
		next if ( /\@\@!erase\s*\@\@/io );

		s/\@\@index-name\@\@/${list[0][0]}/gio;
		s/\@\@index-alt\@\@/${list[0][1]}/gio;
		s/\@\@heading\@\@/${list[0][2]}/gio;

		s/\@\@filename\@\@/${list[$j][0]}/gio;
		s/\@\@prev-name\@\@/${list[$j-1][0]}/gio;
		s/\@\@next-name\@\@/${list[$j+1][0]}/gio;

		s/\@\@alt\@\@/${list[$j][1]}/gio;
		s/\@\@prev-alt\@\@/${list[$j-1][1]}/gio;
		s/\@\@next-alt\@\@/${list[$j+1][1]}/gio;

		s/\@\@title\@\@/${list[$j][2]}/gio;
		s/\@\@type\@\@/${list[$j][3]}/gio;

		s/([\200-\377])/$HTML::Entities::char2entity{$1}/go;

		if ( s/\@\@!include\s*([-\w\.]+)\s*\@\@/\@\@/gio )
		{
			$file = (($1 =~ /^BODY$/io) ? ${list[$j][0]} : $1);
			$file =~ s/html$/body/o ;
			$idx = index($_,"@@");
			print OUT substr( $_, $[, $idx) if ($idx > $[);
			$remain = substr( $_, $idx+2);	# to be printed later
			&process_file( $file);
			print OUT $remain;
		}else{
			print OUT ;
		}
	}
	close IN;
}


sub slurp_list
{
	open( IN, "< html.list") or die "Cannot open html.list\n";
	$j = $[; while ( <IN> )
	{
		chop;
		s/[ \t]*#.*//o;
		tr/\t//s;
		next if ( $_ eq "");
		push @list, [ split(/\t/) ];
		${list[$j][0]} .= "html" if (${list[$j][0]} =~ /\.$/o);
		$j++;
	}
	close IN;
	# adds name anchor to link to index
	${list[0][0]} .= "#${list[0][3]}" if ${list[0][3]};
	# new row to point forward to index
	push @list, [ (${list[0][0]},${list[0][1]}) ];
}

# script starts here
my $oldpwd = `pwd`;
my $source_dir = ".";
my $dest_dir = "/tmp";
$source_dir = $ARGV[0] if scalar(@ARGV) == 2;
$dest_dir = $ARGV[1] if scalar(@ARGV) == 2;
$dest_dir = $ARGV[0] if scalar(@ARGV) == 1;

chdir $source_dir or die "Cannot chdir to $source_dir: $!\n";
die "Destination dir $dest_dir doesn't exist\n" if ! -d $dest_dir;

&slurp_list;

for $j ( 1 .. ($#list - 1) )	# first and last rows point to index
{
	$filename = "${dest_dir}/$list[$j][0]";
	open( OUT, "> ${filename}") or die "Cannot open output file ${filename}\n";

	&process_file( "html.wrapper");

	close OUT;
}
END { chdir $oldpwd; }