#!/usr/local/bin/perl -w use strict ; use Carp ; use YAML ; my @markup = ( { 'search' => 'M<[^<>]+>', 'replace' => sub { my ( $text ) = @_; $text =~ s|M<([^<>]+)>|$1|sg; $text; }, }, { 'search' => 'QUOTE<(.*?)>', 'replace' => sub { my ( $text ) = @_; $text =~ /QUOTE<(.*?)>/gs; my $before = $`; my $after = $'; my $quote = $1; $quote =~ s/\\/
/sg; $before . "

" . "
$quote" . "
" . $after; }, }, ); my ( @sections, $header_text, $page_title_base ); set_header_text() ; process_faq_text() ; process_sections() ; print_section_page() ; exit ; sub process_faq_text { my ( $section, $quest_text, $answer_text, $curr_faq ) ; while( <> ) { next if /^\s*$/ ; s/\n/ /; if ( /^([SQ]):\s*(.+)$/ ) { if ( $curr_faq ) { $curr_faq->{'answer'} = markup_text( $answer_text ) ; $answer_text = '' ; unless ( $curr_faq->{'question'} && $curr_faq->{'answer'} ) { die "bad FAQ entry before line $. in $ARGV\n" ; } push( @{$section->{'faqs'}}, $curr_faq ) ; $curr_faq = undef ; } if ( $1 eq 'S' ) { my $section_title = $2 ; push( @sections, $section ) if $section ; $section = { 'plain_title' => $section_title, 'title' => markup_text( $section_title ), } ; next ; } $quest_text = $2 ; next ; } if ( /^A:\s*(.+)$/ ) { $answer_text = markup_text( $1 ) ; $curr_faq = { 'question' => markup_text( $quest_text ), } ; $quest_text = '' ; next ; } if ( $quest_text ) { $quest_text .= $_ ; next ; } $answer_text .= $_ ; } push( @sections, $section ) ; } sub process_sections { my $sect_num = 1 ; foreach my $sect_ref ( @sections ) { my $title = $sect_ref->{'title'} ; $sect_ref->{'num'} = $sect_num ; my $link = <$title LINK $sect_ref->{'link'} = $link ; my $quest_num = 1 ; foreach my $faq_ref ( @{$sect_ref->{'faqs'}} ) { my $quest = $faq_ref->{'question'} ; my $answer = $faq_ref->{'answer'} ; $faq_ref->{'num'} = $quest_num ; $faq_ref->{'index'} = "$sect_num.$quest_num" ; $faq_ref->{'link'} = <$quest LINK $quest_num++ ; } $sect_num++ ; } } sub print_section_page { my $page_text = < title => "$page_title_base" Home > FAQ


Frequently Asked Questions

"; write_file( 'faq.html', $page_text ) ; } sub print_faq_pages { my ( $sect_ref ) = @_ ; my $quest_list ; my $faq_text ; my $plain_title = $sect_ref->{'plain_title'} ; my $title = $sect_ref->{'title'} ; my $sect_num = $sect_ref->{'num'} ; my $page_text = < title => "$page_title_base > $plain_title" Home > FAQ > $title

$title


HTML $quest_list .= < HTML foreach my $faq_ref ( @{$sect_ref->{'faqs'}} ) { my $quest = $faq_ref->{'question'} ; my $answer = $faq_ref->{'answer'} ; my $faq_num = $faq_ref->{'num'} ; my $faq_ind = $faq_ref->{'index'} ; $quest_list .= <$faq_ref->{'link'} HTML $faq_text .= <

$quest

$answer

HTML } $quest_list .= "" ; my $section_list = '" ; $page_text .= $section_list ; $page_text .= $faq_text ; write_file( "faq$sect_num.html", $page_text ) ; } sub set_header_text { $page_title_base = 'Stem Systems, Inc. > Stem > FAQ' } sub write_file { my( $file_name ) = shift ; local( *FH ) ; open( FH, ">$file_name" ) || carp "can't create $file_name $!" ; print FH @_ ; } sub markup_text { my ( $text ) = @_; map { if ($text =~ /$_->{'search'}/s) { $text = $_->{'replace'}->($text); } } @markup; return $text; } __END__