#!/usr/bin/perl

# This is a basic starter template script for processing your xml. There are some
# very basic checks at the beginning that will apply to most xml but you may need
# to alter these checks if your xml deviates too much from the norm.
# origin: http://digfir-published.macmillanusa.com/brucetest/asset/process_xml.pl


use utf8;
use strict;
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");


# GLOBALS that may be useful

my $BOOK_ID = "krugmanwellsmicro4";
my $PUBLISHER = "fw"; # set this to either "bsm" or "fw"
my $CHAPTER;
my $CHAPTER_TITLE;

# Use '_NL_' to specify newlines (such as for question formatting)
my $NEWLINE = "_NL_";

if ($BOOK_ID eq "") {
    print STDERR "Please set the BOOK_ID variable in this script\n";
    exit();
}
if ($PUBLISHER ne "bsm" && $PUBLISHER ne "fw") {
    print STDERR "Please set the PUBLISHER variable in this script to either 'bsm' or 'fw'\n";
    exit();
}

print STDERR "BOOK_ID: $BOOK_ID\n";
print STDERR "PUBLISHER: $PUBLISHER\n";

#######################################################
# code to process from original xml file

my $chno = $ARGV[0];
my $chid = $chno;
print STDERR "read in command line argv: $ARGV[0] chapter no: $chno\n";

my %partsappampm = ('fm' => 100, 'part1' => 101, '2A' => 102, '9A' => 109, '10A' => 110, '19A' => 119, );
#if ($chno =~ m`(part|fm|\d+A)`i) {
#	$chid = $partsappampm{$chno};
#	print STDERR "processing $ARGV[0] --> $chno\n";
#}
my	$xmlfilesource;
my	$xmlfileid;
my 	$xmlfileprocessed;
if ($chno =~ m`fm`) {
	$xmlfilesource =  "fm/fm.xml";
	$xmlfileid  = "fm/fm_id.xml";
	$xmlfileprocessed = "fm/fm_processed.xml";
} else {
	$xmlfilesource = sprintf ("ch%02s/ch%02s.xml",$chno, $chno);
	$xmlfileid  = sprintf ("ch%02s/ch%02s_id.xml",$chno, $chno);
	$xmlfileprocessed = sprintf ("ch%02s/ch%02s_processed.xml",$chno, $chno);
}

print STDERR "add_ids.pl ch$chid < $xmlfilesource > $xmlfileid\n";
     system ("perl add_ids.pl ch$chid < $xmlfilesource > $xmlfileid");

#my $xmlfileprocessed = sprintf ("ch%02s/ch%02s_processed.xml",$chno, $chno);
print STDERR $xmlfileprocessed;
close STDOUT;
open STDOUT, ">$xmlfileprocessed" or die "Can't open STDOUT: $!";

#######################################################

my $contents;
my $temp_line;
#while ($temp_line = <STDIN>) {
open XMLFILEID, "< $xmlfileid" or die "can't open sec##_id.xml $xmlfileid";
while ($temp_line = <XMLFILEID>) {
    chomp($temp_line);

    $temp_line =~ s`<\?xml [^>]+>``g;
    $temp_line =~ s`<(\/)?manuscript( [^>]+)?>``g;

    $temp_line =~ s/[\t\r\n]//g; #remove tabs, line feeds, returns
    $temp_line =~ s/[ ]{2,}/ /g; # remove multiple spaces
    $temp_line =~ s/^\s+//; # remove leading spaces
    $contents .= $temp_line;
}


# If you want to "remove" content from your xml without actually deleting it from
# the file, wrap it in <rm>...</rm>
# WARNING: do not embed <rm> tags inside of <rm> tags!


### front matter
if ($CHAPTER == "fm") {

	#<p id="krugmanwellsmicro4-fm-para-1" block_type="page_start">i</p>
	#s`<p id="[^"]*" block_type="page_start">i</p>`</section><section id="${BOOK_ID}_fm_page_i" chapter="fm" title="" numbered="" level="2" printed_page="i">`;
	#$contents = s`<p id="[^"]*" block_type="page_start">ii</p>`</section><section id="${BOOK_ID}_fm_page_iii" chapter="fm" title="" numbered="" level="2" printed_page="iii">`; # page ii has nothing
	#$contents = s`<p id="[^"]*" block_type="page_start">iii</p>`<section id="${BOOK_ID}_fm_page_iii" chapter="fm" title="" numbered="" level="2" printed_page="iii">`;
	#$contents = s`<p id="krugmanwellsmicro4-fm-para-53" block_type="page_start">iv</p>`</section><section id="${BOOK_ID}_fm_page_iv" chapter="fm" title="" numbered="" level="2" printed_page="iv">`;
	#$contents = s`<p id="krugmanwellsmicro4-fm-para-100" block_type="page_start">v</p>`</section><section id="${BOOK_ID}_fm_page_v" chapter="fm" title="" numbered="" level="2" printed_page="v">`;

}
###<p id="krugmanwellsmicro4-ch3-p-26" block_type="page_start">69</p>
$contents =~ s`(<p id="[^"]*" block_type="page_start">[^<]*</p>)`<rm>$1</rm>`g;

$contents =~ s`<rm>.*?</rm>``g;


# Fix up any title attributes that have inline tags
$contents =~ s` title="([^"]+)"`" title=\"" . fix_title($1) . "\""`ge;

# Change <tag> to {tag}
sub fix_title {
    my $title = shift;
    $title =~ s`<([^>]+)>`{$1}`g;
    return $title;
}


$_ = $contents;


# At this point, the xml from the file is now one big string with no newlines ##########


### mdash can't have space following
s`&#8212;\s`&#8212;`g;

### chapter 16
### <link href="krugmanwellsmicro4_ch1_1.html" target="_self">Chapter 11</link>
#s`(chapter)\s+(\d+a?)`<link href="${BOOK_ID}_ch${2}_1.html" target="_self">$1 $2</link>`ig;

### <list are missing attribute type which is required by DF
s`(<list id="[^"]*" block_type="(green_|orange_|)?bullet")>`$1 type="unordered">`g;
s`(<list id="[^"]*" block_type="lower-alpha_bold")>`$1 type="ordered">`g;
s`(<list id="[^"]*" block_type="numbered(_bold)?")>`$1 type="ordered">`g;

### block_type="bx_v4"><p> --> block_type="term"
s`block_type="bx_v4"><p`block_type="term"><p`g;

### <box id="krugmanwellsmicro4-ch3-box-19" block_type="bx_v4" title="Quick Review">
s`block_type="bx_v\d" title="Quick Review"`block_type="bx_qreview" title="Quick Review"`g;

### <box id="krugmanwellsmicro4-ch3-box-20" block_type="bx_v6" title="Check Your Understanding 3-1">
s`block_type="bx_v6" title="Check Your Understanding (\d+-\d+)"`block_type="bx_cyu" title="$1"`g;

### box_metadata not expected by DF
s`box_metadata`box-metadata`g;
s`block_type="[^"]*" title="[^"]*"><box_metadata><title>(!(worldview)!)? ?ECONOMICS <em>in Action</em>: `block_type="eia$2" title="Economics in Action: `g;
s`</title></box_metadata>`">`g;
###<box-metadata><box-title>!worldview! ECONOMICS <em>in Action</em>: Boy or Girl? It Depends on the Cost</box-title></box-metadata>
s`block_type="[^"]*" title="[^"]*">(<box[-_]metadata><box-title>(!(worldview)!)? ?ECONOMICS <em>in Action</em>: (.*?)</box-title></box[-_]metadata>)`block_type="eia$3" title="Economics in Action: $4">$1`g;

### level 4 section: change them to subheads
###<section id="krugmanwellsmicro4-ch3-section-8" chapter="3" title="Changes in Income" numbered="false" level="3" print_page="74">
s`</section><section id="[^"]*" chapter="\w+" title="([^"]*)" numbered="\w+" level="3" [^>]*>(<p[^>]*>)`$2<phrase block_type="subhead">$1</phrase> `g;

### equations in paragraph: set id for <p
###<p id="krugmanwellsmicro4-ch6-para-392" block_type="txt_tac"><image src="kwe4e_ch6_eqn185-01.jpg"/></p>
s`<p id="[^"]*"([^>]*)>(<image\s+id="[^"]*"\s+src="([^\.]*)\.jpg"\s*/>)`<p id="$3" block_type="txt_eqn">$2`g;

# Get chapter info/check for start-numbering-at
s`<chapter ([^>]+)>`"<chapter " . get_chapter_info($1) . ">"`e;

sub get_chapter_info {
    my $tag = shift;

    ($CHAPTER_TITLE) = $tag =~ /title="([^"]+)"/;
	print STDERR "tag:$tag\n";
	# krugmanwellsmicro4 xml came with title="3: Supply and Demand" instead of number=""
    ($CHAPTER) = $tag =~ /title="(?:ch)?(?:[0]+)?(\d+( appendix)?|intro):? /i;

    if ($CHAPTER_TITLE eq "") {
	print STDERR "Can't find chapter title\n";
	exit();
    }
    if ($CHAPTER eq "") {
	# <chapter title="INTRO: Introduction: The Ordinary Business of Life">
	if ($tag =~ m`krugmanwellsm[ai]cro4-(ch)?(fm)`i) {
		$CHAPTER = $2;
	} else {
		print STDERR "Can't find chapter number [tab:${tag}]\n";
		exit();
	}
    }
	if ($CHAPTER=~ m`(appendix|intro)`i) {
		$CHAPTER = $chid;		# see partsappampm
	}

    # If chapter does not have start-numbering-at attribute then add it
    if ($tag !~ /start-numbering-at/) {
	$tag .= qq` start-numbering-at="${CHAPTER}"`;
    }
    # Otherwise, make sure start-numbering-at is the same as chapter number
    else {
	$tag =~ s`start-numbering-at="[^"]+"`start-numbering-at="${CHAPTER}"`;
    }

    return $tag;
}



### Uncomment the following lines if your vendor supplied IDs need fixing
### (i.e. they are not unique enough)
### The fix_id subroutine my also need fixing depending on the format of the IDs

#s`id="([^"]+)"`"id=\"" . fix_id($1) . "\""`ge;
#s`refid="([^"]+)"`"refid=\"" . fix_id($1) . "\""`ge;

sub fix_id {
    my $id = shift;

    if ($id !~ /^$BOOK_ID/) {
	return $BOOK_ID . "-ch" . $CHAPTER . "-" . $id;
    }

    return $id;
}




# BEGINNING THE PROCESS FUNCTIONS
# This script is designed to cycle through all the major elements in the xml file.
# For each element there is a corresponding "process" function where you can add your
# custom code.






# "pre-process" sections
# Anything you need to do to a section before you begin processing the elements
# within that section goes in the pre_process_section function
s`<section ([^>]+)>(.*?)</section>`pre_process_section($1, $2)`ge;


# process boxes
# put all of your box processing code in the process_box function
my $BOX_COUNT = 0;
while (/(<box [^>]+>)/) {
    s`<box ([^>]+)>((?:(?!<box ).)*?)</box>`process_box($1, $2)`ge;
    $BOX_COUNT++;
    if ($BOX_COUNT > 1000) {
	print STDERR "Too many boxes ...\n";
	print STDERR $1, "\n";
	exit(0);
    }
}
# these are important, don't remove them
s`<boxer `<box `g;
s`</boxer>`</box>`g;


# process paras
# put all of your para processing code in the process_para function
s`<p ([^>]+)>(.*?)</p>`process_para($1, $2)`ge;


# process figures
# put all of your figure processing code in the process_figure function
s`<figure ([^>]+)>(.*?)</figure>`process_figure($1, $2)`ge;


# process lists
# put all of your list processing code in the process_list function
my $LIST_COUNT = 0;
while (/(<list [^>]+>)/) {
    s`<list ([^>]+)>((?:(?!<list ).)*?)</list>`process_list($1, $2)`ge;
    $LIST_COUNT++;
    if ($LIST_COUNT > 1000) {
	print "Too many lists ...\n";
	exit(0);
    }
}
# these are important, don't remove them
s`<lister `<list `g;
s`</lister>`</list>`g;


# process tables
# put all of your table processing code in the process_table function
my $TABLE_COUNT = 0;
while (/(<table [^>]+>)/) {
    s`<table ([^>]+)>((?:(?!<table ).)*?)</table>`process_table($1, $2)`ge;

    $TABLE_COUNT++;
    if ($TABLE_COUNT > 1000) {
	print "Too many tables ...\n";
	exit(0);
    }
}
# these are important, don't remove them
s`<tabler `<table `g;
s`</tabler>`</table>`g;


# post process section
# Anything you need to do to a section after you have processed the elements
# within that section goes in the post_process_section function
s`<section ([^>]+)>(.*?)</section>`post_process_section($1, $2)`ge;

# convert tags in rawhtml for DF
s`<rawhtml>(.*?)</rawhtml>`"<rawhtml>" . fix_up_rawhtml($1) . "</rawhtml>"`ge;

# add new lines back in to xml
#s`(<(rawhtml|chapter|section|box|layout|table|tbody|tr|list|figure|layout|question|image|asset_source)(/| [^>]+)?>)`\1\n`g;
s`(<(rawhtml|chapter|section|box|layout|table|tbody|tr|list|figure|layout|question|asset_source)(/| [^>]+)?>)`\1\n`g;
s`(</(metadata|p|div|table|tr|td|list|li|box|question|query|tbody|th|rawhtml|caption|figure|section-metadata)>)`\1\n`g;
s`</section>`</section>\n`g;
s`</rawhtml>`\n</rawhtml>`g;


# restore newlines
s`${NEWLINE}`\n`g;


### print out the xml ###

print $_, "\n";


sub fix_up_rawhtml {
    my $text = shift;
    $text =~ s`<`&lt;`g;
    $text =~ s`>`&gt;`g;
    return $text;
}



sub pre_process_section {
    my $args = shift; # attributes on the <section> tag
    my $content = shift; # content of section

    # Fix title if there are inline tags (e.g. {em})
    if ($args =~ /title="([^"]+)"/ && $content !~ /<section-metadata>/) {
	my $title = $1;

	if ($title =~ /\{[^}]+\}/) {
	    $title =~ s`\{([^}]+)\}`<${1}>`g;
	    $content = "<section-metadata><section-title>${title}</section-title></section-metadata>" . $content;
	    $args =~ s`title="[^"]+"`title=""`;
	}
    }

	### front matter specific	
	if ($CHAPTER == "fm") { 
		$content =~ s`<table `<tabler `g; #fm has huge table4 that seems going over the regex match limit
		$content =~ s`</table>`</tabler>`g;

		if ($args =~ m'Fm Introduction'i) {
			my %globaleia = ('10' => "<--", '39' => "<--", '78' => "<--", '85' => "<--", '95' => "<--",'110' => "<--", '11' => "<--", '124' => "<--", 
							'177' => "<--", '140' => "<--", '226' => "<--", '233' => "<--",
							'180' => "<--", '242' => "<--", '263' => "<--", '378' => "<--", '392' => "<--", '406' => "<--", '425' => "<--", '431' => "<--", 
							'477' => "<--", '492' => "<--", '502' => "<--", '536' => "<--", '596' => "<--",
							'21' => "<--", '45' => "<--", '244' => "<--", '440' => "<--", '506' => "<--", '538' => "<--", ); # last row is for business casetable
			while ($content =~ s`> ?([&#;\?\w ,:]*, (\d+)) ?<`!GT!__EIA_TITLE__!LT!`) { #__lt__$1__gt__
				my $eiatitle = $1; 
				my $pno = $2;	#print STDERR "[fm intro title eia title]${eiatitle} p ${pno} $globaleia{$pno}\n";
				if (exists $globaleia{$pno}) {
					$eiatitle = qq`!LT!phrase block_type="introtable_eiablue"!GT!${eiatitle}!LT!/phrase!GT!`;	
				} else {
					$eiatitle = qq`!LT!phrase block_type="introtable_eiablack"!GT!${eiatitle}!LT!/phrase!GT!`;	
				}
				$content =~ s`__EIA_TITLE__`$eiatitle`;
			}
			$content =~ s`!LT!`<`g;
			$content =~ s`!GT!`>`g;
			$content =~ s`<strong>(\d:)</strong>`<phrase block_type="introtable_cno1">$1</phrase>`ig;
			$content =~ s`<strong>(\d:) NEW:</strong>`<phrase block_type="introtable_cno1">$1</phrase><phrase block_type="introtable_new">NEW:</phrase>`ig; #<strong>5: NEW:</strong>
			$content =~ s`<strong>(\d\d:)</strong>`<phrase block_type="introtable_cno2">$1</phrase>`ig;
			$content =~ s`<strong>(\d\d:) NEW:</strong>`<phrase block_type="introtable_cno2">$1</phrase><phrase block_type="introtable_new">NEW:</phrase>`ig;
			$content =~ s`<strong>(NEW:)</strong>`<phrase block_type="introtable_new">$1</phrase>`ig;
			#$content =~ s`</phrase>([^<]*)<image`</phrase><phrase block_type="introtable_eiatitle">$1</phrase><image`ig;
		}	
		if ($args =~ m`title="BRIEF CONTENTS"`) {
			#<strong><phrase block_type="smallcap">part</phrase> 1</strong>
			$content =~ s`<strong>(<phrase block_type="smallcap">part</phrase>) (\d+)</strong>`$1<phrase block_type="partnum">$2</phrase>`g;
		}
		if ($args =~ m`title="CONTENTS"`) {
			#<strong><phrase block_type="smallcap">introduction</phrase> The Ordinary Business of Life</strong>
			$content =~ s`<strong>(<phrase block_type="smallcap">introduction</phrase>) (The Ordinary Business of Life)</strong>`$1<phrase block_type="content_ctitle">$2</phrase>`;
			#<strong><phrase block_type="smallcap">chapter</phrase> 1 First Principles</strong>
			$content =~ s`<strong>(<phrase block_type="smallcap">chapter</phrase>) (\d+) (.*?)</strong>`$1<phrase block_type="content_chno">$2</phrase><phrase block_type="content_ctitle">$3</phrase>`g;
			my $c = 1;
			$content =~ s`<p id="kwe4e_arrow" block_type="txt_eqn">`sprintf '<p id="%s_fm_contents_%s_chap_arrow" block_type="txt_eqn">',${BOOK_ID},${c}++`ge;
			$content =~ s`<strong><phrase block_type="smallcap">for inquiring minds</phrase>:</strong>`<phrase block_type="content_inquiringminds">FOR INQUIRING MINDS:</phrase>`g;	
			$content =~ s`<strong>ECONOMICS</strong> <image`<phrase block_type="content_eia1">ECONOMICS </phrase><image`g;
			$content =~ s`<em>IN ACTION</em>`<phrase block_type="content_eia2">IN ACTION</phrase>`g;
			$content =~ s`<strong>BUSINESS</strong> CASE:`<phrase block_type="content_bcase1">BUSINESS</phrase><phrase block_type="content_bcase2"> CASE:</phrase>`g;
			$content =~ s`<strong><phrase block_type="smallcap">global comparison</phrase>:</strong>`<phrase block_type="content_gcomp">GLOBAL COMPARISON:</phrase>`g;
			#<p id="krugmanwellsmicro4-fm-para-182" block_type="txt"><strong><phrase block_type="smallcap">part</phrase> 1</strong> What Is Economics?</p>
			$c = 1;
			$content =~ s`<p id="[^"]*" block_type="txt"><strong>(<phrase block_type="smallcap">part</phrase>) (\d+)</strong>(.*?)</p>`sprintf '<p id="%s_fm_contents_%s_parttitle" block_type="txt">%s<phrase block_type="content_partno">%s</phrase><phrase block_type="content_parttitle">%s</phrase></p>',${BOOK_ID},${c}++,$1,$2,$3`ige;
		}
		if ($args =~ m`title="The Fourth Edition`i) { #print STDERR "[preface]\n\n";
			# <p id="krugmanwellsmicro4-fm-para-754" block_type="txt"><phrase block_type="subhead">Many New Examples and Stories with an Emphasis on Currency</phrase>
			$content =~ s`(<p id="([^"]*)" [^>]*><phrase block_type="subhead">.*?</phrase>)`$1</p><p id="${2}-2" block_type="txt">`g; #$1</p><p id="$2" block_type="txt">
		}
		#<p id="krugmanwellsmicro4-fm-para-1175" block_type="txt"><image id="krugmanwellsmicro4-chfm-image-163" src="kwe4e_sign_1.jpg"/></p>
		$content =~ s`(src="kwe4e_sign_1.jpg"/>)`$1<phrase block_type="authorname">Paul Krugman</phrase>`;
		$content =~ s`(src="kwe4e_sign_2.jpg"/>)`$1<phrase block_type="authorname">Robin Wells</phrase>`;
		$content =~ s`<p id="kwe4e_square" block_type="txt_eqn">`<p id="kwe4e_square" block_type="squarecode">`;
	}
	
	### don't want section numbers in b-heads as shown in pdf
	$args =~ s`numbered="true"`numbered="false"`;

	### 
	$args =~ s`level="2"`level="2" block_type="b-head"`;

	### chapter specific images e.g. src="kwe4e_ch3_fig_6.jpg
	$content =~ s`src="`src="asset/ch$CHAPTER/`g;

	### id="krugmanwellsmicro4-ch3-section-1" chapter="3" title="Chapter Introduction"
	if ($args =~ m`-sec(tion)?-1"` && $args =~ m`title="Chapter Introduction"`i) {
	#if ($args =~ m`-sec(tion)?-1"` && $args =~ m`title="Chapter Introduction"`i && $args !~ m`-ch\d+A-sec-1`i) {
		$args =~ s`title="[^"]*"`title="${CHAPTER_TITLE}" block_type="section_intro"`;
		$content =~ s`block_type="bx_v5" title="!arrow! What You Will Learn in This Chapter"`block_type="bx_whatyouwilllearn" title="!arrow! What You Will Learn in This Chapter"`;
		$content =~ s`(<box id="krugmanwellsmicro4-ch\d+-box-2" [^>]*><figure [^>]*>(.*?)</figure>)`$1</box><box id="${BOOK_ID}-ch$CHAPTER-intro-text" block_type="intro_text">`;
		if ($args =~ m`krugmanwellsm[ai]cro4-intro`) {
			$content =~ s`(<box id="krugmanwellsm[ai]cro4-intro-box-1"[^>]*>.*?</box>)`$1<box id="${BOOK_ID}-ch$CHAPTER-intro-text" block_type="intro_text">`;
			$content =~ s`$`</box>`;
		}
		if ($args !~ m`-ch\d+A-sec-1`i) { #no drop cap for appendice
			$content =~ s`(<p id="[^"]*" block_type="txt">)((&#\d+;)?\w)`$1<phrase block_type="drop_cap">$2</phrase>`; #first letter in intro paragraph 
			$content =~ s`(<p id="[^"]*" block_type="txt_in">)((&#\d+;)?\w)`$1<phrase block_type="drop_cap">$2</phrase>`; #first letter in intro paragraph 
		}
	}
	#<box id="krugmanwellsmicro4-ch3-box-9" block_type="bx_v3" title="!earth! GLOBAL COMPARISION: Pay More, Pump Less">
	#<box id="krugmanwellsmicro4-ch3-box-43" block_type="bx_v3" title="BUSINESS CASE: An Uber Way to Get a Ride">
	#<box id="krugmanwellsmicro4-ch3-box-45" block_type="bx_v3" title="!launch! WORK IT OUT">
	$content =~ s`block_type="bx_v3" title="((!earth! )?GLOBAL COMPARISION:?)`block_type="bx_gcomparison" title="`g;
	$content =~ s`block_type="bx_v3" title="(!worldview!)? ?BUSINESS CASE: ?`block_type="bx_businesscase" title="$1`g;
	$content =~ s`block_type="bx_v3" title="!launch! WORK IT OUT"`block_type="bx_workitout" title="WORK IT OUT"`g;
	$content =~ s`block_type="bx_v5" title="PITFALLS:`block_type="bx_pitfalls" title="PITFALLS:`g;
	$content =~ s`block_type="bx_v5"( title="(!worldview!)? ?FOR INQUIRING MINDS:)`block_type="bx_inquiryminds"$1`g;

	# <section id="krugmanwellsmicro4-ch3-section-32" chapter="3" title="SUMMARY"
	if ($args =~ s`id="[^"]*" ((chapter="[^"]*" )?title="SUMMARY")`id="${BOOK_ID}-section-summary-ch${CHAPTER}" $1`) {
		$content =~ s`<list id="[^"]*"`<list id="${BOOK_ID}-section-summary-list-ch${CHAPTER}"`;
	}

	# <section id="krugmanwellsmicro4-ch3-section-32" chapter="3" title="KEY TERMS"
	if ($args =~ s`id="[^"]*" ((chapter="[^"]*" )?title="KEY TERMS")`id="${BOOK_ID}-section-keyterms-ch${CHAPTER}" block_type="section_keyterms" $1`) {
		$content =~ s`<list id="[^"]*"`<list id="${BOOK_ID}-section-summary-list-ch${CHAPTER}"`;
	}

	# <section id="krugmanwellsmicro4-ch3-section-32" chapter="3" title="PROBLEMS"
	if ($args =~ s`id="[^"]*" ((chapter="[^"]*" )?title="PROBLEMS")`id="${BOOK_ID}-section-problems-ch${CHAPTER}" $1`) {
		if ($content !~ m`^<box `) { # missing box wrapping around questions
			$content = qq`<box id="krugmanwellsmicro4-ch${CHAPTER}-box-problems-addback" block_type="bx_v6">` . $content;
			$content =~ s`</question>(<box [^>]*>)`</question></box>$1`;
		}
		$content =~ s`<list id="[^"]*"`<list id="${BOOK_ID}-section-summary-list-ch${CHAPTER}"`;
		#<box id="krugmanwellsmicro4-ch3-box-44" block_type="bx_v6"><question
		$content =~ s`<box id="[^"]*" block_type="bx_v6"><question`<box id="${BOOK_ID}-problems-box-ch$CHAPTER" block_type="bx_problems"><question`;
		$content =~	s`<question (id="krugmanwells(econ4|micro4|macro4)-ch\d+A?-question-\d+" )?(number="(\d+A?)[-_\.](\d+)")>`<question id="${BOOK_ID}-problems-ch$4-$5" $3 block_type="chapter_problem">`g;
	}

	# Key Terms
	if ($args =~ m`level="1"` && $args =~ m`title="Key Terms"`i) {
	#<p id="krugmanwellsmodulesmicro3-ch1-p-308">Factor markets, p. 12</p>
		my $term = "";
		my $tid = "";
		#<p>Marginal revenue product of labor (<em>MRPL</em>), p. 445</p> 
		#while ($content =~ s`<p ([^>]*)>([^<]*)</p>`__A_TERM__`) {
		while ($content =~ s`<p ([^>]*)>(.*?)</p>`__A_TERM__`) {
			my $argp = $1;
			$term = $2;
			$term =~ s`</?strong>``g;
			$term =~ s`[,\.]? p\. \d+``;
			#$tid =~ s`[\s<>-_\(\)]``g;
			$term = "<p_term $argp><termref>$term</termref></p_term>";
			$content =~ s`__A_TERM__`$term`;
		}
		# create key term activity <question...> block
		my $termsActivity = create_term_question_block ($args, $content);
		$content = $termsActivity . $content;
		$content = $termsActivity;
	}

    return qq`<section ${args}>${content}</section>`;
}


sub post_process_section {
    my $args = shift; # attributes on <section> tag
    my $content = shift; # content of section

    # move term boxes up one para by default
    $content =~ s`(<p [^>]+>(?:(?!<p ).)*?</p>)(<box [^>]+ block_type="term">.*?</box>)`\2\1`g;

    # Fix title if there are inline tags
    if ($args =~ /title="[^"]+"/ && $content !~ /<section-metadata>/) {
	my $title = $1;
	if ($title =~ /<[^>]+>/) {
	    $content = "<section-metadata><section-title>${title}</section-title></section-metadata>" . $content;
	    $args =~ s`title="[^"]+"`title=""`;
	}
    }


	#$content =~ s`([Ff]igure (\d+)[-_](\d+[abc]?))`sprintf '<link href="asset/Sec%s/c%02s_fig%02s.html" target="_pop">%s</link>',$chno,$2,$3,$1`eg;
	my %app_figCh = ('2A' => 102, '9A' => 109, '10A'=> 110, '19A'=> 119);
	while ($content =~ s`([Ff]igure ([A\d]+)[-_](\d+[abc]?))`_FIG_LINK_`) {
		my $theFigLink = $1;
		my $figCh = $2;  #if (exists ($app_figCh{$figCh})) {$figCh = $app_figCh{$figCh};}
		my $figNo = $3;
		$theFigLink =~ s`([Ff]igure) `_$1_link_`;
		#my $linkUrl = sprintf ('asset/Sec%02s/c%02s_fig%02s.html',$CHAPTER,$figMod,$figNo);
		my $linkUrl = sprintf ('asset/ch%s/kwe4e_ch%s_fig_%s.html',$figCh,$figCh,$figNo); #kwe4e_ch3_fig_7.html
		#my $linkUrl = sprintf ('asset/ch%s/kwe4e_ch%s_fig_%s.html',$app_figCh{$figCh},$figCh,$figNo); #kwe4e_ch3_fig_7.html
		#if ($bookid =~ m`krugmanwellsmodulesmacro3` and $CHAPTER eq "2" and $figMod =~ m`[456]`) {
		#	$linkUrl = sprintf ('asset/Sec%02s_micro/c%02s_fig%02s.html',$CHAPTER,$figMod+1,$figNo);
		#	print STDERR "[post_process_section] figure links $figMod-$figNo url changed to -> $linkUrl\n";
		#}
		$content =~ s`_FIG_LINK_`<link href="$linkUrl" target="_pop">$theFigLink</link>`;
	}
	$content =~ s`_([Ff]igure)_link_`$1 `g;
	$content =~ s`(figures )(\d+)-(\d+) and (\d+)-(\d+)`sprintf '%s<link href="asset/ch%s/kwe4e_ch%s_fig_%s.html" target="_pop">%s-%s</link> and <link href="asset/ch%s/kwe4e_ch%s_fig_%s.html" target="_pop">%s-%s</link>',$1,$2,$2,$3,$2,$3,$4,$4,$5,$4,$5`ige;
	$content =~ s`(figures )(\d+)-(\d+), (\d+)-(\d+), and (\d+)-(\d+)`sprintf '%s<link href="asset/ch%s/kwe4e_ch%s_fig_%s.html" target="_pop">%s-%s</link>, <link href="asset/ch%s/kwe4e_ch%s_fig_%s.html" target="_pop">%s-%s</link>, and <link href="asset/ch%s/kwe4e_ch%s_fig_%s.html" target="_pop">%s-%s</link>',$1,$CHAPTER,$2,$3,$2,$3,$CHAPTER,$4,$5,$4,$5, $CHAPTER,$6,$7,$6,$7`ige;


	#$content =~ s`([Tt]able (\d+)[-_](\d+[abc]?))`sprintf '<link href="asset/Sec%s/ch%s_tab%02s.html" target="_pop">%s</link>',$chno,$2,$3,$1`eg;
	while ($content =~ s`([Tt]able (\d+)[-_](\d+[abc]?))`_TABLE_LINK_`) {
		my $theTabLink = $1;
		my $tabCh = $2;
		my $tabNo = $3;
		$theTabLink =~ s`([Tt]able) `_$1_link_`;
		#my $linkUrl = sprintf ('asset/Sec%02s/ch%s_tab%02s.html',$CHAPTER,$tabMod,$tabNo);
		my $linkUrl = sprintf ('asset/ch%s/kwe4e_ch%s_tab_%s.html',$CHAPTER,$tabCh,$tabNo); #kwe4e_ch3_tab_2.html
		#if ($bookid =~ m`krugmanwellsmodulesmacro3` and $CHAPTER eq "2" and $tabMod =~ m`[456]`) {
		#	$linkUrl = sprintf ('asset/Sec%02s_micro/ch%s_tab%02s.html',$CHAPTER,$tabMod+1,$tabNo);
		#}
		$content =~ s`_TABLE_LINK_`<link href="$linkUrl" target="_pop">$theTabLink</link>`;
	}
	$content =~ s`_([Tt]able)_link_`$1 `g;
	$content =~ s`(tables )(\d+)-(\d+) and (\d+)-(\d+)`sprintf '%s<link href="asset/ch%s/kwe4e_ch%s_tab_%s.html" target="_pop">%s-%s</link> and <link href="asset/ch%s/kwe4e_ch%s_tab_%s.html" target="_pop">%s-%s</link>',$1,$CHAPTER,$2,$3,$2,$3,$CHAPTER,$4,$5,$4,$5`ige;
	$content =~ s`(tables )(\d+)-(\d+), (\d+)-(\d+), and (\d+)-(\d+)`sprintf '%s<link href="asset/ch%s/kwe4e_ch%s_tab_%s.html" target="_pop">%s-%s</link>, <link href="asset/ch%s/kwe4e_ch%s_tab_%s.html" target="_pop">%s-%s</link>, and <link href="asset/ch%s/kwe4e_ch%s_tab_%s.html" target="_pop">%s-%s</link>',$1,$CHAPTER,$2,$3,$2,$3,$CHAPTER,$4,$5,$4,$5, $CHAPTER,$6,$7,$6,$7`ige;

	$content =~ s`(chapter)\s+(\d+a?)`<link href="${BOOK_ID}_ch${2}_1.html" target="_self">$1 $2</link>`ig;





    # Force browsers to line break correctly at dashes.
    # NOTE: This is still experimental.  Please let Bruce know if it
    # causes any problems.
	if ($args !~ m`"key terms"`i) {
    $content = fix_linebreaks($content);
	}
    return qq`<section ${args}>${content}</section>`;
}

###########################################################
sub process_figure {
    my $args = shift; # attributes on <figure> tag
    my $content = shift; # contents of figure

	### unnumbered figures
	###	figure id="krugmanwellsmicro4-ch3-figure-9" numbered="false" mmtype="image" mmsrc="" attr="">
	### <layout align="right" width="medium" border="true"/>
	### <image id="krugmanwellsmicro4-ch3-image-9" src="kwe4e_ch3_un_03.jpg"/>
	### <figure id="krugmanwellsmicro4-intro-fig-1" numbered="false" mmtype="image" mmsrc="asset/ch101/" attr="">
	### <image id="krugmanwellsmicro4-ch101-image-1" src="asset/ch101/kwe4e_intro_un_01.jpg"/>
	### <figure id="krugmanwellsmicro4-fm-fig-2" numbered="false" mmtype="image" mmsrc="" attr="">
	### <image id="krugmanwellsmicro4-chfm-image-152" src="kwe4e_Fm_un_2.jpg"/>
	if ($content =~ m`<image id="[^"]*" src="(asset/ch\d+/)?kwe4e_((ch\d+|intro)_un_[^\.]*)\.(gif|jpg)"`i || $content =~ m`src="(asset)/chfm/kwe4e_(Fm_un_\d+)\.jpg`i) {
		print STDERR "process_figre... unnumbered figure $2\n";
		my $figid = $2;
		if ( $args =~ m`-cyu-`) {
			$args =~ s`id="([^"]*)"`id="$1" block_type="fig_unnumbered"`;
		} else {
			$args =~ s`id="[^"]*"`id="${BOOK_ID}-unnumbered_fig-$figid" block_type="fig_unnumbered"`;
		}
		$content =~ s`<image id="[^"]*"`<image id="${BOOK_ID}-unnumbered_img-$figid"`;
		$content =~ s`<caption><strong>`<caption>`;
		$content =~ s`</strong></caption>`</caption>`;
	}


	### numbered figures
	### <figure id="krugmanwellsmicro4-ch3-figure-14" numbered="false" mmtype="image" mmsrc="" attr="">
	### <layout align="center" width="medium" border="true"/>
	### <image id="krugmanwellsmicro4-ch3-image-14" src="kwe4e_ch3_fig_10.jpg"/>
	### kwe4e_ch2A_fig_1.jpg
	if ($content =~ m`<image id="[^"]*" src="(asset/ch\d+A?/)?kwe4e_(ch\d+A?_fig_([^\.]*))\.jpg"`i) {
		print STDERR "process_figre...... numbered figure $2";
		my $figid = $2;
		my $fignum = $3;
		$args =~ s`id="[^"]*"`id="krugmanwellsmicro4-numbered_fig-$figid" block_type="fig_numbered"`;
		$content =~ s`<image id="[^"]*"`<image id="krugmanwellsmicro4-numbered_img-$figid"`;
		my ($figtitle) = $content =~ m`<caption><strong>(.*?)</strong>`; print STDERR "[$figtitle]\n";
		#$args =~ s`attr=""`attr="$CHAPTER-$fignum:$figtitle"`;
	}
	### numbered table
	### <figure id="krugmanwellsmicro4-ch3-figure-8" numbered="false" mmtype="image" mmsrc="asset/ch3/" attr="">
	### <layout align="center" width="medium" border="true"/>
	### <image id="krugmanwellsmicro4-ch3-image-8" src="asset/ch3/kwe4e_ch3_tab_1.jpg"/>
	### <image id="krugmanwellsmicro4-ch3-image-8" src="kwe4e_ch3_tab_1.jpg"/>
	if ($content =~ m`<image id="[^"]*" src="(asset/ch\d+/)?kwe4e_(ch\d+_tab_([^\.]*))\.jpg"`i) {
		print STDERR "process_table...... numbered table $2";
		my $tabid = $2;
		my $tabnum = $3;
		$args =~ s`id="[^"]*"`id="krugmanwellsmicro4-numbered_tab-$tabid" block_type="tab_numbered"`;
		$content =~ s`<image id="[^"]*"`<image id="krugmanwellsmicro4-numbered_img-$tabid"`;
		#<caption>TABLE 10-3 Sammy&#8217;s Marginal Utility per Dollar</caption>
		#<table_caption>TABLE 14.1 The HHI for Some Oligopolistic Industries</table_caption>
		#$content =~ m`(<caption>(<strong>)?TABLE (\d+)[-\.](\d+)? ?(.*?)(</strong>)?</caption>)`i; print STDERR "[caption:$1|$3|$4]\n";
		$content =~ m`(<(table_)?caption>(<strong>)?TABLE (\d+)[-\.](\d+)? ?(.*?)(</strong>)?</(table_)?caption>)`i; print STDERR "[caption:$1|$4|$5]\n";
		#$args =~ s`attr=""`attr="$CHAPTER-$tabnum:$tabtitle"`;
		$args = $args . qq` data-tno="$4-$5" data-ttitle="$6"`;
		my ($tabtitle) = $3; print STDERR "[args:$args]\n";
	}


    # Create supp html files for figures
    my ($img_file_no_ext) = $content =~ /src="(?:[^"]+\/)?(.*?)\.(?:jpg|gif|png|tiff)"/;
	#$img_file_no_ext =~ s`ch(\d+A)`ch$chid`;
    create_supp_file("figure", $img_file_no_ext, $args, $content);

	$content =~ s`(<image )`${NEWLINE}$1`;
    return qq`<figure ${args}>${content}</figure>`;
}



###########################################################
sub process_list {
    my $args = shift; # attributes on <list> tag
    my $content = shift; # contents of list




    # yes, we are returning "lister"
    return qq`<lister ${args}>${content}</lister>`;
}


###########################################################
sub process_table {
    my $args = shift; # attributes on <table> tag
    my $content = shift; # contents of table

    # process each <td>
    #$content =~ s`(<td[^>]*>.*?</td>)`process_td($1)`ge;
    $content =~ s`(<t[dh][^>]*>.*?</t[dh]>)`process_td($1)`ge;
	my $box_wrap0 = "";
	my $box_wrap1 = "";

	if ($CHAPTER == "fm" && $args=~ m`krugmanwellsmicro4-fm-table-4`) {
		print STDERR "[process_table 0]$args\n$content\n\n\n";

	} else {	
    # Create table supp html files for numbered tables.  You may need to adjust
	#print STDERR "[process_table_1]$content\n"; 
    # the regex for your particular xml.
	# <table_caption><p id="krugmanwellsmicro4-ch1-para-19" block_type="txt"><strong>TABLE 1-1 
	# <table_caption><p id="krugmanwellsmicro4-ch7-para-422" block_type="txt"><strong>TABLE 7-6 Comparison Shopping for <em>A Deadly indifference</em> by Marshall Jevons</strong></p></table_caption>
	#<table_caption><p id="krugmanwellsmicro4-ch9-para-31" block_type="txt">TABLE 9-1 Opportunity Cost of an Additional Year of School</p></table_caption>
    #if ( $content =~ m`<table_caption>(<p[^>]*>)?(<strong>)?TABLE (\d+)-(\d+) ([^<]*)(</strong>)?`i ) {
	my ($tcaption) =  $content =~ m`<table_caption>(.*?)</table_caption>`;
    if ( $tcaption ) {
	$tcaption =~ s`<((/)?em)>`!$1!`g;
	$tcaption =~ s`(<p[^>]*>)?(<strong)? ?TABLE (\d+A?)[-\.](\d+)(.*)?(</p>)?(</strong>)?`$5`i;
	my $tno = "$3-$4";
	my $table_file_no_ext = "kwe4e_ch$3_tab_$4";
	my $table_title = $5; $table_title=~ s`(</strong>)?</p>``;
	print STDERR "[process_table_numbered]$tno:$table_title\ttable_file_no_ext:$table_file_no_ext\n";
	$table_file_no_ext = lc($table_file_no_ext);
	$args =~ s`$` data-tno="$tno" data-ttitle="${table_title}"`;
	#$args =~ s`data-tno="table[ _-]`data-tno="`;
	$args =~ s`block_type="tb_v1"`block_type="tab_numbered"`;
	$table_file_no_ext =~ s`[ \-\.]`_`g;
	#print STDERR "[process_table_2]create_supp_file: $table_file_no_ext ($args)\n$content\n\n"; 
	create_supp_file("table", $table_file_no_ext, $args, $content);
	# numbered table need to be clickable and open supp window
	$box_wrap0 = qq`<box id="${table_file_no_ext}_tablewrapperbox" data-href="asset/ch${CHAPTER}/${table_file_no_ext}.html" data-target="_pop">${NEWLINE}`;
	$box_wrap1 = "${NEWLINE}</box>";
    } else {
		if ($args !~ s`block_type="[^"]*"`block_type="tab_unnumbered"`) {
			$args =~ s`(id="[^"]*")`$1 block_type="tab_unnumbered"`;
		}
		#print STDERR "[process_table_unnumbered] $args\n";
		my ($table_file_no_ext) = $args =~ m`id="([^"]*)"`;
		$table_file_no_ext = lc($table_file_no_ext);
		$table_file_no_ext =~ s`[ \-\.]`_`g;
		create_supp_file("table", $table_file_no_ext, $args, $content);
		$box_wrap0 = qq`<box id="${table_file_no_ext}_tablewrapperbox" data-href="asset/ch${CHAPTER}/${table_file_no_ext}.html" data-target="_pop">${NEWLINE}`;
		$box_wrap1 = "${NEWLINE}</box>";

	}

		### special cases
		### 11-1 CYU q 1b: table is tagged for show answer
		### id="krugmanwellsmicro4-cyu-11-1-1b" data-show-answer="array"
		my %table_showanswer = (
				'krugmanwellsmicro4-cyu-4-1-1a' => 1, 'krugmanwellsmicro4-cyu-4-2-1a' => 1,
				'krugmanwellsmicro4-cyu-11-1-1b' => 1, 'krugmanwellsmicro4-cyu-11-3-1b' => 1,
				'krugmanwellsmicro4-cyu-20-1-1a' => 1,);
		my ($tid) = $args =~ m`id="([^"]*)"`;
		#if ($args =~ m`id="krugmanwellsmicro4-cyu-11-1-1b" data-show-answer="array"`) {
		if (exists $table_showanswer{$tid}) {
			print STDERR "[process_table] special case: ${tid}\n\n";
			$args =~ s`id="${tid}" data-show-answer="array"`id="${tid}-table"`;
			$box_wrap1 = qq`${box_wrap1}${NEWLINE}<p id="${tid}" data-show-answer="array"> </p>`;
		}

	}

    # yes, we are returning "tabler"
    #return qq`<tabler ${args}>${content}</tabler>`;
    return qq`${box_wrap0}<tabler ${args}>${content}</tabler>${box_wrap1}`;
}


sub process_td {
    my $td = shift;
	
	#<td align="center"><p id="krugmanwellsmicro4-ch18-para-206" block_type="txt">&#160;&#160;&#160;8.56%</p></td>
	$td =~ s`(<p id="[^"]*") block_type="txt"`$1`;

    return $td;
}


sub process_box {
    my $args = shift; #attributes on <box> tag
    my $content = shift; # contents of box

	# <box id="krugmanwellsmicro4-ch3-intro-text" block_type="intro_text"> #see pre_process_section
	if ($args=~ m`block_type="intro_text"`) {
		$content =~ s`</p>\s*<p[^>]*>`<br/><phrase block_type="p_indent"></phrase>`g;	
	}	

	# block_type="bx_gcomparison"
	if ($args =~ m`bx_gcomparison`) {
		#$content =~ s`(<figure [^>]*>.*?</figure>)``;
		#$content = "$1$content"; 
#<p id="krugmanwellsmicro4-ch14-para-276" block_type="txt_in"><em>Sources:</em> European Commission, Department of Justice Workload Statistics; PACIFIC Exchange Rate Service at University of British Columbia.</p>
		$content =~ s`(<p id="[^"]*") block_type="txt(_in)?">(<em>Sources?:</em>)`$1 block_type="txt_source">$3`;
		$content =~ s`(<p id="[^"]*" block_type="txt">)((&#\d+;)?\w)`$1<phrase block_type="drop_cap">$2</phrase>`; #first letter in intro paragraph 
	}

	# block_type="bx_inquiryminds add to last </p>
	if ($args =~ m`bx_inquiryminds`) {
		$content =~ s`(<figure.*?</figure>)\s*$`__LAST_FIGURE__`;
		my $last_fig = $1;
		if ($content =~ m`__LAST_FIGURE__`) {
			$content =~ s`</p>__LAST_FIGURE__$`<phrase block_type="inquirymind_end_square"></phrase></p>__LAST_FIGURE__`; #print STDERR "\n\n\n$content\n\n\n";
		} else {
			$content =~ s`</p>$`<phrase block_type="inquirymind_end_square"></phrase></p>`;
		}
		$content =~ s`__LAST_FIGURE__`${last_fig}`;
	}

	# Pitfalls
	if ($args =~ m`block_type="bx_pitfalls" title="(PITFALLS:) ?([^"]*)"`i) {
		my $title = $2;
		$content =~ s`</p>\s*<p[^>]*>`<br/><phrase block_type="p_indent"></phrase>`g;
		my $pitfalls_id = $title;
		$pitfalls_id=~ s`[:punct:]``g;
		$pitfalls_id =~ s` ``g;
		$content =~ s`(^<p[^>]*>)`<p id="${BOOK_ID}-pitfalls-banner-ch$CHAPTER-${pitfalls_id}"><phrase block_type="pitfalls_banner">PITFALLS</phrase></p>$1<phrase block_type="pitfalls_title">$title</phrase><br/>`;
	}

	# Economics In Action
	# block_type="bx_v5" title="!worldview! ECONOMICS <em>in Action</em>: Beating the Traffic"
	#if ($args =~ s` block_type="bx_v5" title="!worldview! ECONOMICS <em>in Action</em>:([^"]*)"`block_type="eia_world" title="$1"`) {
	if ($args =~ m`id="([^"]*)" block_type="eia[^"]*" title="(Economics in Action: )?([^"]*)"`i) {
		my $boxid = $1;
		my $eiatitle = $3;
		if ($content !~ m`^(<figure [^>]*>.*?</figure>)?<p [^>]*>(<strong>)`) {
			print STDERR "\nadding eia title: $eiatitle... 2\n";
			#$content = qq`<p id="${BOOK_ID}-eia-title"><strong>$eiatitle</strong></p>$content`;
			if ($content =~ m`<box-metadata`) {
				$content =~ s`(</box-metadata>)`$1<p id="${boxid}-eia-title"><strong>$eiatitle</strong></p>__EIA_START__`;
			} else {
				$content = qq`<p id="${boxid}-eia-title"><strong>$eiatitle</strong></p>__EIA_START__$content`;
			}
		}
		#$content =~ s`<p id="([^>]*)" block_type="txt(_in)?">`<p id="$1" block_type="txt">`; #first <p... should be block_type="txt"
		if ($content =~ m`__EIA_START__<table`) {
			$content =~ s`</table><p id="([^>]*)" block_type="txt(_in)?">(\w)`</table><p id="$1" block_type="txt"><phrase block_type="dropcap_eia">$3</phrase>`;
		}
		if ($content =~ m`__EIA_START__<figure`) {
			$content =~ s`</figure><p id="([^>]*)" block_type="txt(_in)?">(\w)`</figure><p id="$1" block_type="txt"><phrase block_type="dropcap_eia">$3</phrase>`;
		}
		if ($content =~ m`__EIA_START__<p`) {
			$content =~ s`__EIA_START__<p id="([^>]*)" block_type="txt(_in)?">(\w)`<p id="$1" block_type="txt"><phrase block_type="dropcap_eia">$3</phrase>`; #first <p... should be block_type="txt" & first letter 
		}
		$content =~ s`__EIA_START__``g; }

	# Check Your Understanding 
	# <box id="krugmanwellsmicro4-ch3-box-20" block_type="bx_cyu" title="3-1">
	# <li><p id="krugmanwellsmicro4-cyu-3-1-1"  data-show-answer="array" numbered="false" title="">A store owner finds that customers are willing to pay more for umbrellas on rainy days.</p></li>
	if ($args =~ m`block_type="bx_cyu" title="([^"]*)"`) {
		my $cyutitle = $1;
		my $qcount = 1;
		my $cyuid;
		# process each question in box <question id="krugmanwellsmicro4-ch3-question-1" number="3.1">
		#while ($content =~ s`<question id="[^"]*" (number="(\d+)[-_\.](\d+)")>(.*?)</question>`<question_cyu id="${BOOK_ID}-cyu-$cyutitle-$3" $1>_cyu_q_</question>`) {
		#while ($content =~ s`<question (id="[^"]*" )?(number="(\d+)[-_\.](\d+)")>(.*?)</question>`<question_cyu id="${BOOK_ID}-cyu-$cyutitle-$4" $1>_cyu_q_</question>`) {
		while ($content =~ s`<question (id="[^"]*" )?(number="(\d+)[-_\.](\d+)")>(.*?)</question>`<li><question_cyu id="${BOOK_ID}-cyu-$cyutitle-$4" $2>_cyu_q_</question></li>`) {
			my $qno = $4;
			my $qcontent = $5;
			$cyuid = "${BOOK_ID}-cyu-$cyutitle";
			my $count = 0;
			my $order = chr(ord ('a')+$count);
			if ($qcontent =~ s`<list id="[^"]*"`<list id="${cyuid}-${qno}_list"`) { # each <li is a sub question
				#my $count = 0;
				#$order = chr(ord ('a')+$count);
				#while ($qcontent =~ s`<li><p [^>]*>`<li_q><p id="${cyuid}-$qno$order"  data-show-answer="array" numbered="false" title="">`) {
				while ($qcontent =~ s`(<li>.*?</li>)`_SUB_Q_`) {
					my $subq = $1;
					if ($subq =~ m`<table `) {
						$subq =~ s`<li><p [^>]*>`<li_q><p id="${cyuid}-$qno$order-p" numbered="false">`;
						$subq =~ s`<table id="[^"]*" ([^>]*)>`<table id="${cyuid}-$qno$order" data-show-answer="array" $1>`;
					} else {
						$subq =~ s`<li><p [^>]*>`<li_q><p id="${cyuid}-$qno$order"  data-show-answer="array" numbered="false" title="">`;
					}
					$count = $count + 1;
					$order = chr(ord ('a')+$count);
					$qcontent =~ s`_SUB_Q_`$subq`;
				}
				$qcontent =~ s`<li_q>`<li>`g;
			} else { # <question id="krugmanwellsmicro4-cyu-2-1-3" number="2.3"><p id="krugmanwellsmicro4-ch2-para-157" block_type="txt">
				if ($qcontent =~ m`<(figure|table) `) { # ASSUMING only one of the two in questions and is the last thing in question
					$qcontent =~ s`<p [^>]*>`<p id="${cyuid}-$qno$order-p" numbered="false" title="">`;
					$qcontent =~ s`<table id="[^"]*" ([^>]*)>`<table id="${cyuid}-$qno$order" data-show-answer="array" $1>`;
					$qcontent =~ s`<figure id="[^"]*" ([^>]*)>`<figure id="${cyuid}-$qno$order" data-show-answer="array" $1>`;
				} else {
					$qcontent =~ s`<p [^>]*>`<p id="${cyuid}-$qno$order" data-show-answer="array" numbered="false" title="">`;
				}
			}
			$content =~ s`_cyu_q_`$qcontent`;
		}
		$content =~ s`<question_cyu`<question`g;
		$content =~ s`(<li><question)`<list id="${cyuid}-biglist" type="ordered" block_type="numbered_bold">$1`;
		#$content =~ s`(<p id="[^"]*" block_type="txt_tar">Solutions appear at back of book\.</p>)`</list>$1`;
		$content =~ s`(<p id="[^"]*" block_type="txt_tar">Solutions appear at back of book\.</p>)``;
		$content =~ s`$`</list><p id="${cyuid}-solutionatback" block_type="txt_tar">Solutions appear at back of book\.</p>`;
		# <li><question id="krugmanwellsmicro4-cyu-1-3-1" ><p id="krugmanwellsmicro4-ch1-para-236" block_type="txt"><strong>1</strong>. 
		$content =~ s`(<li><question[^>]*><p[^>]*>)<strong>\d+</strong>\. ?`$1`g;
	}
	
	# Business Case: Questions for Thought
	if ($args =~ m`block_type="bx_businesscase"`) {
		$content =~ s`(<p [^>]*><phrase block_type="sub_h1">QUESTIONS? FOR THOUGHT</phrase></p>)`$1<list id="${BOOK_ID}-qforthought-list-ch$CHAPTER" block_type="lower-alpha_bold" type="ordered">`;
		my $qid = "${BOOK_ID}-qforthought-ch$CHAPTER";
		$content =~ s`<question (id="[^"]*" )?number="(\d+)[-\.](\d+)"><p [^>]*>(.*?)</p></question>`<li><question id="$qid-$3-q" number="$2-$3" numbered="true"><query id="${qid}-$3">ES: $4</query><metadata><field key="question_report_text">$4</field></metadata></question></li>`g;
		$content = "$content</list>";
	}
	
	# question box in PROBLEMS section
	# <box id="krugmanwellsmicro4-problems-box-ch3" block_type="bx_problems">
	if ($args =~ m`block_type="bx_problems"`) {
		$content =~ s`<question `<li><question `g;
		$content =~ s`</question>`</question></li>`g;
		$content =~ s`(<li><p id="[^"]*") block_type="txt"`$1`g;
		$content = qq`<list id= "krugmanwellsmicro4-problems-list-ch$CHAPTER" block_type="numbered" type="ordered">$content`;
		$content =~ s`$`</list>`;
		$content =~ s`(<figure[^>]*>)<layout align="right"`$1<layout align="center"`g;
	}

	# question box in Work it Out box
	# <box id="krugmanwellsmicro4-ch3-box-46" block_type="bx_v6"><question
	if ($args =~ m`block_type="bx_workitout"`) {
		print STDERR "bx_workout\n";
		#$content =~ s`<(box(er)?) id="[^"]*" block_type="bx_v6">\s*<question id="[^"]*" (number="(\d+A?)[-_\.](\d+))`<$1 id="${BOOK_ID}-workout-questionbox-ch$CHAPTER" block_type="bx_workout_question"><question id="${BOOK_ID}-workitout-question-ch${CHAPTER}-$5" $3`;
		#<p id="krugmanwellsmicro4-ch15-para-251" block_type="bx_workitout_intro"><strong>For interactive, step-by-step help solving the following problem, check out this Work It Out tutorial under student resources.</strong></p><boxer id="krugmanwellsmicro4-workout-questionbox-ch15" block_type="bx_workout_question">
		$content =~ s`<p id="([^"]*)" block_type="txt(_in)?">(<strong>For interactive,.*?)</p>(<box[^>]*>)?`$4<p id="$1" block_type="bx_workitout_intro"><strong>For interactive, step-by-step help solving the following problem, check out this Work It Out tutorial under student resources.</strong></p>`i;
		print STDERR "[process_box] bx_workout:[pid:$1][box:$4][boxid:$6][text:$3]\n";
		$content =~ s`<question id="[^"]*" (number="(\d+A?)[-_\.](\d+))`<question id="${BOOK_ID}-workitout-question-ch${CHAPTER}-$3" $1`;
		if ($content !~ s`<(box(er)?) id="[^"]*" block_type="bx_v6">`<$1 id="${BOOK_ID}-workout-questionbox-ch$CHAPTER" block_type="bx_workout_question">`) {
			$content =~ s`(<p id="[^"]*" block_type="bx_workitout_intro">)`<boxer id="${BOOK_ID}-workout-questionbox-ch$CHAPTER" block_type="bx_workout_question">$1`;
			$content =~ s`</question>`</question></box>`;
		}
		$content =~ s`(<li><p id="[^"]*") block_type="txt">`$1>`g; #txt_in
		#$content =~ s`<p id="([^"]*)" block_type="txt(_in)?">(<strong>For interactive,.*?)</p>(<box(er)? id="[^"]*" block_type="bx_workout_question">)?`$4<p id="$1" block_type="bx_workitout_intro"><strong>For interactive, step-by-step help solving the following problem, check out this Work It Out tutorial under student resources.</strong></p>`i;
		# <question id="krugmanwellsmicro4-workitout-question-ch13-17" number="13.17" block_type="chapter_problem"><p id="krugmanwellsmicro4-ch13-para-511" block_type="txt">
		# --> <p id="krugmanwellsmicro4-ch13-para-511" block_type="woq"><phrase block_type="wo_qno">17.</phrase> 
		$content =~ s`(<question id="[^"]*" number="\d+A?\.(\d+)"[^>]*>\s*<p id="[^"]*")[^>]*>`$1 block_type="woq"><phrase block_type="wo_qno">$2.</phrase> `;
		print STDERR "$content\n\n\n";
	}


    # yes, we are returning "boxer"
    return qq`<boxer ${args}>${content}</boxer>`;
}

### question content can have multiple <p...>, <figure ...>, <table>.
### add question specific id and data-show-answer="array" to the last element
###
sub process_question {
	my $args = shift;
	my $content = shift;



}


sub process_para {
    my $args = shift; #attributes on <p> tag
    my $content = shift; # contents of para



    return qq`<p ${args}>$content</p>`;
}


#
# create_term_question_block
#
sub create_term_question_block {
    my $args = shift; # attributes on the <section> tag
    my $content = shift; # content of section

	#$content =~ s`<p[^>]*><termref>([^<]*)</termref></p[^>]*>`$1\t`g;
	$content =~ s`<p[^>]*><termref>(.*?)</termref></p[^>]*>`$1\t`g;
	my @terms = split ('\t', $content);
	#print STDERR "terms in key terms section:@terms";

	open TERMFILE, "<terms.js" or die "Can't load terms.js: $!\n";
	#print STDERR "opened terms.js\n";

	my $termsJS="";
	my @termsjs = <TERMFILE>;
	foreach my $termjs (@termsjs) {
		#print STDERR "$termjs\n";
    	$termjs =~ s/[\t\r\n]//g; #remove tabs, line feeds, returns
    	$termjs =~ s/[ ]{2,}/ /g; # remove multiple spaces
    	$termjs =~ s/^\s+//; # remove leading spaces
		$termsJS .= $termjs;
	}
	#print STDERR "\nterms from js file:$termsJS\n";
	
	my $termMatchBlock = "";
	my $i = 0;
	foreach my $t (@terms) { 
		if ($t =~ m`^\d+$`) {next;}
		my $tmp = $t;
		# xBookUtils.terms['marginal revenue product of labor (<em>MRPL</em>)'] = 
		$tmp =~ s`([\(\)])`\\$1`g;
		my ($termDef) = $termsJS =~ m`\['$tmp'\] = "([^"]*)";`i;
		#print STDERR "${t}::$termDef\n";
		$termMatchBlock .= chr($i%10+65) . ".${t}::${termDef}_NL_"; 
		$i = $i +1;
	}

	$termMatchBlock = qq`<question id="${BOOK_ID}-keyterm-matching-sect${CHAPTER}" numbered="false" number="">_NL_<query id="${BOOK_ID}-keyterm-matching-sect${CHAPTER}-query">_NL_MA:_NL_${termMatchBlock}</query></question>_NL_`;
	#print STDERR "====\n$termMatchBlock\n";

	return $termMatchBlock;
	

}




# This function will automatically create subdirectories to store the supp
# html files it produces as follows:

# figures: img_chN/ (where 'N' is the chapter number specified in the xml)
# tables: table/

sub create_supp_file {
    my $type = shift; # 'figure' or 'table'
    my $filename = shift; # the name of the supp file we are creating
    my $args = shift; # the attributes of the tag
    my $content = shift; # the content in the tag

    if ($filename eq "" || $filename =~ /\s/) {
	print STDERR "Bad filename for supp file [args: $args]\n";
	return;
    }

    # determine write file
    my $write_file;

    if ($type eq "figure") {
	# if only the filename is passed then add the default sub directory
	if ($filename !~ /\//) {
	    my $sub_dir = "img_ch${CHAPTER}";
	    if (!-d $sub_dir) {
		mkdir($sub_dir);
	    }
	    $write_file = $sub_dir . "/" . $filename;
	    $write_file =~ s`\.([a-z]+)$`.html`;
	    if ($write_file !~ /\.html$/) {
		$write_file .= ".html";
	    }
	}
	# otherwise use the filename passed in
	else {
	    $write_file = $filename;
	}
    }
    elsif ($type eq "table") {
	if ($filename !~ /\//) {
	    my $sub_dir = "table";
	    if (!-d $sub_dir) {
		mkdir($sub_dir);
	    }
	    $write_file = $sub_dir . "/" . $filename;
	    $write_file =~ s`\.([a-z]+)$`.html`;
	    if ($write_file !~ /\.html$/) {
		$write_file .= ".html";
	    }
	}
	else {
	    $write_file = $filename;
	}
    }
    else {
	print STDERR "Bad type ($type) passed to create_supp_file\n";
	return;
    }

    $content =~ s`<link (href="[^"]+")>(.*?)</link>`<span data-type="link" data-$1>$2</span>`g;

    if (!open(OUT, ">${write_file}")) {
	print STDERR "create_supp_file: Can't write to ${write_file}: $!\n";
	return;
    }

    binmode(OUT, ":utf8");

    my $supp_content;

    if ($type eq "figure") {

	my ($block_type) = $args =~ /block_type="([^"]+)"/;
	my ($id) = $args =~ /id="([^"]+)"/; #$id = "${id}-supp";
	my ($attr) = $args =~ m`attr="([^"]*)"`;
	my ($ttitle) = $args =~ m`data-ttitle="([^"]*)"`;
	my ($tno) = $args =~ m`data-tno="([^"]*)"`;
	my $figLayout = "";

	$content =~ s`<figure [^>]+>`<div data-type="figure">\n`;
	$content =~ s`<phrase block_type="head">(.*?)</phrase>`<span data-block_type="head">\1</span>`;
	$content =~ s`<asset_source>(.*?)</asset_source>`<div data-type="asset_source">\1</div>\n`;

	# We assume there is only one <caption> per <figure>
	$content =~ s`<caption>(.*?)</caption>`<div data-type="figure_text"><span data-type="caption">\1</span></div>\n`;

	$content =~ s`</figure>`</div>\n`;
	if ($block_type =~ m`fig_unnumbered`) {
		$content =~ m`<layout ([^>]+)>`;
		$figLayout = $1;
		$figLayout =~ s` border="true"``;
		$figLayout =~ s`align="[^"]*"`data-layout-align="left"`;
		$figLayout =~ s`width="[^"]*"`data-layout-width="large"`;
	}
	$content =~ s`(<layout [^>]+>)``;
	$content =~ s`src="(?:[^"]+\/)?(.*\.(?:jpg|gif|png|tiff))"`src="$1"`g;
	$content =~ s`src="asset/[^/]*/`src="`g;

	#$content =~ s`<image `<img data-attr="${attr} "`;

	$supp_content = qq`<div data-type="figure" data-figure-id="${id}" data-block_type="${block_type}" data-ttitle="${ttitle}" data-tno="${tno}" ${figLayout}>${content}</div>`;

    }
    elsif ($type eq "table") {

	my ($block_type) = $args =~ /block_type="([^"]+)"/;
	my ($id) = $args =~ /id="([^"]+)"/; $id = "$id-supp";
	my ($layout) = $content =~ /<layout(?:[^>]+)?align="([^"]+)"(?:[^>]+)?\/>/;
	my ($width) = $content =~ /<layout(?:[^>]+)?width="([^"]+)"(?:[^>]+)?\/>/;
	my ($border) = $content =~ /<layout(?:[^>]+)?border="([^"]+)"(?:[^>]+)?\/>/;
	my ($number) = $args =~ /number="([^"]+)"/;
	my ($ttitle) = $args =~ m`data-ttitle="([^"]*)"`;
	my ($tno) = $args =~ m`data-tno="([^"]*)"`;

	$content =~ s`<layout[^>]+>``;

	$content =~ s`<table_caption>(.*?)</table_caption>`<div data-type="table_text"><span data-type="number">Table ${number}: </span><span data-type="table_caption">$1</span></div>`;

	$supp_content = qq`<div data-type="table" data-block_type="${block_type}" id="${id}" data-layout-align="${layout}" data-layout-width="${width}" data-layout-border="${border}" data-mmsrc="" data-attr="" data-ttitle="${ttitle}" data-tno="${tno}"><table width="auto" height="auto">${content}</table></div>`;
    }

    $supp_content =~ s`<phrase block_type="([^"]+)">`<span data-block_type="\1">`g;
    $supp_content =~ s`</phrase>`</span>`g;

    my $pub_ext = "";
    if ($PUBLISHER eq "fw" || $PUBLISHER eq "FW") {
	$pub_ext = "_fw";
    }

    $supp_content = fix_linebreaks($supp_content);
	my $ch = lc($CHAPTER);
	if ($ch !~ "fm") {
		$ch = "ch${ch}"
	} 

    print OUT<<ENDL;
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class="no-js"> <!--<![endif]-->
<head>
    <title></title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


<link href="../../css/boilerplate.css" media="screen" rel="stylesheet" type="text/css" >
<link href="../../css/jquery-ui-custom.css" media="screen" rel="stylesheet" type="text/css" >
<link href="../../css/manuscript.css" media="screen" rel="stylesheet" type="text/css" >
<link href="../../css/digfir_ebook${pub_ext}.css" media="screen" rel="stylesheet" type="text/css" >
<link href="../../css/${BOOK_ID}.css" media="screen" rel="stylesheet" type="text/css" >
<link href="../../css/${BOOK_ID}_${ch}.css" media="screen" rel="stylesheet" type="text/css" >
</head>

<body id="supp_win">
<div id="manuscript" data-chapter-number="${CHAPTER}">
<div data-type="section">

${supp_content}

</div>
</div><!-- /#manuscript -->
<br class="clear" />
<!-- end of material in original view -->

<script type="text/javascript" src="../../js/utilities.js"></script>
<script type="text/javascript" src="../../js/query_types.js"></script>
<script type="text/javascript" src="../../js/player.js"></script>
<script type="text/javascript" src="../../js/jquery.js"></script>
<script type="text/javascript" src="../../js/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="../../js/jquery_extensions.js"></script>
<script type="text/javascript" src="../../js/swfobject.js"></script>
<script type="text/javascript" src="http://admin.brightcove.com/js/BrightcoveExperiences.js"></script>
<script type="text/javascript" src="../../js/digfir_ebook${pub_ext}.js"></script>
<script type="text/javascript" src="../../js/${BOOK_ID}.js"></script>
<script type="text/javascript" src="../../js/${BOOK_ID}_${ch}.js"></script>
<script type="text/javascript">
    //<!--

\$(window).ready(function () {
	player.initialize('52c6e83c757a2ef70c000000');
});
    //-->
</script>

</body>
</html>
ENDL


close(OUT);
} # end create_figure_supp_file


# Adds <phrase block_type="nobr"> around (em/en)dash and letter preceding it.
# Adds <wbr> after (em/en)dash.  This forces all browswers to line break after
# the dash.
# While we are at it, add <wbr> after forward slash in URLs.
# This does not fix anything inside <rawhtml>.
sub fix_linebreaks {
    my $text = shift;
    my $new_text = "";
    my @tokens = split(/(<[^>]+>)/,$text);

    my $token;
    my $protected = 0;
    foreach $token (@tokens) {

	# look for tags first
	if ($token =~ /<(rawhtml|link)/) {
	    $protected = $1;
	}
	elsif ($token =~ /<\/${protected}/) {
	    $protected = 0;
	}
	elsif ($token =~ /</) {
            # nothing to do here, this is just a tag
	}
	# if we've made it this far we have text
	elsif (!$protected) {
	    # the last char in the pattern is a utf emdash
	    $token =~ s`([a-zA-Z0-9](&#8211;|&#8212;|-|—))`<phrase block_type="nobr">$1</phrase><wbr/>`g;
	}
	elsif ($protected eq "link") {
	    $token =~ s`([^/]/)`${1}<wbr/>`g;
	}
	$new_text .= $token;
    }

    return $new_text;
}


__END__

(NOTE: Do not put any perl code below.  All perl code should go above __END__)

CHANGE LOG

2014-02-04: Script now creates supplemental html files for images and numbered tables.
 - added $PUBLISHER variable (must be set to either "bsm" or "fw")
 - added create_supp_file()
 - added calls to create_supp_file() in process_figure() and process_table()

2014-02-14: Fixed bug found by Kevin in process_figure where pattern
for finding $img_file_no_text was too greedy (added '?')

2014-06-02: Added fix_linebreaks() to force browsers to break line correctly
at (em/en)dash

2014-06-02: Added check for inline tags in section titles

2014-06-24: Fixed bug in post_process_section: moved $args =~ s`title="[^"]+"`title=""`; inside of if block

2014-08-15: Added call to fix_linebreaks() in create_supp_file()
