#!/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.


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

#GET VIDEOS FILE
my %videoLinks = do 'videoLinks2.pl';

# GLOBALS that may be useful

my $BOOK_ID = "pulsipherconcepts3e";
my $BOOK_ID_SHORT = "puls3e";
my $PUBLISHER = "fw"; # set this to either "bsm" or "fw"
my $CHAPTER;
my $CHAPTER_TITLE;
my %terms = ();
my %answers = ();
my $letter = 'a';
my $level1 = "true";

# 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";

my $contents;
my $temp_line;
while ($temp_line = <STDIN>) {
    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!
$contents =~ s`<rm>.*?</rm>``g;

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

# fix pulse
$contents =~ s`Pulse`puls3e`g;

# 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


# 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="([^"]+)"/;
    ($CHAPTER) = $tag =~ /number="(?:ch)?(?:[0]+)?(\d+)"/i;

    if ($CHAPTER_TITLE eq "") {
	print STDERR "Can't find chapter title\n";
	exit();
    }
    if ($CHAPTER eq "") {
	print STDERR "Can't find chapter number\n";
	exit();
    }

    # 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;
}

# fix src
s` src="([^/]+\.jpg)"` src="asset/img_ch${CHAPTER}/$1"`g;
s`img_ch${CHAPTER}/puls3e_camera.jpg`pics/video.jpg`g;


#Read in Glossary
open my $glossfile, '<', 'glossary/terms.js';
chomp(my @glosslines = <$glossfile>);
close $glossfile;
foreach (@glosslines) {
	my @aparts = split(/'/, $_);
	#if ($_ =~ /xBookUtils.terms[\'([^\']+)\'] = \'([^\']+)\';/) {
		#$answers{$1} = $2;
	#}
	$terms{@aparts[1]} = @aparts[3];
}


# 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>(<p id="[^>]+" block_type="table-note">((?:(?!</p>).)*?)</p>)?(<p id="[^>]+" block_type="table-note">((?:(?!</p>).)*?)</p>)?(<p id="[^>]+" block_type="table-note">((?:(?!</p>).)*?)</p>)?(<p id="[^>]+" block_type="table-note">((?:(?!</p>).)*?)</p>)?(<p id="[^>]+" block_type="table-note">((?:(?!</p>).)*?)</p>)?`process_table($1, $2, $3, $5, $7, $9, $11)`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`(</(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=""`;
	}
    }
	
	# QUESTION SECTIONS
	if ($args =~ /![pP]uls3e_geo! GEOGRAPHIC INSIGHTS/) {
		$args  =~ s`![pP]uls3e_geo! ``g;
		$args = qq`${args} block_type="GI_questions"`;
		$content  =~ s`<li><p id="([^"]+)" block_type="txt">`<li><question id="${1}"><query id="${1}-query">ES: `g;
		$content  =~ s`</p></li>`</query></question></li>`g;
    }
	if ($args =~ /CRITICAL THINKING QUESTIONS/) {
		$args = qq`${args} block_type="CT_questions"`;
		$content  =~ s`<question number="\d\d?.(\d\d?)"><p id="([^"]+)" block_type="txt">`<question id="${2}"><query id="${2}-query">ES: <strong>${1}.</strong> `g;
		$content  =~ s`</p>`</query>`g;
    }
	if ($args =~ /!puls3e_oarr!/) {
		$args  =~ s`!puls3e_oarr! ``g;
		$args = qq`${args} block_type="oarr"`;
    }
	
	if ($CHAPTER == 601) {
		$content  =~ s`block_type="txt"><strong>CHAPTER`block_type="ch_txt"><strong>CHAPTER`g;
	}

	# Title is in orange bar
	#if ($content =~ /!puls3e_geo\d\d?! GEOGRAPHIC INSIGHT \d\d?/) {
	#	$args .= qq` block_type="orange_bar"`;
    #}
	

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


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

	if ($args =~ /title="Chapter Introduction"/) {
		$args =~ s`title="[^"]+"`title="${CHAPTER_TITLE}" block_type="chapter-title"`g;
    }
	
	$content =~ s`<phrase block_type="small"><em>\[((?:(?!\]</em></phrase>).)*?)\]</em></phrase>`<phrase block_type="asset_source">\[\1\]</phrase>`g;
	$content =~ s`<em>\[((?:(?!\]</em>).)*?)\]</em>`<phrase block_type="asset_source">\[\1\]</phrase>`g;
	
    # 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=""`;
	}
    }
	
	# build matching exercise
	if ($args =~ /KEY TERMS/) {
	  $args = qq`${args} block_type="key_terms"`;
	  my @cparts = split("</p>", $content);
	  my $total = @cparts;
	  
# FIND THE MOST EVEN SPLIT  
use POSIX;
my $size = 20;
my $subs = floor($total / $size);
if ($subs < 1) { $subs = 1; }
my $grow = ceil($total % $size / $subs);
my $mysize = $size + $grow;
foreach my $j (1..16) {
  if ($mysize > 22) {
    $mysize = $size - $j;
    $grow = ceil($total % $mysize / $subs);
    $mysize = $mysize + $grow;
  }
}

	  my $i = 0;
	  my $id = 1;
	  $content = "";
	  foreach (@cparts) {
	    my $line = $_;
	    $line =~ s`<p[^>]+><strong>((?:(?!</strong>).)*)</strong>[^<]+`process_term($1)`ge;  ##### HERE'S THE 'MATCH EVERYTHING UP TO...' CODE
		$content = $content . $line;
		$i += 1;
		$id += 1;
		if( $i == $mysize && $id <= $total) {
		  #$j += 1;
		  $i = 0;
		  $letter = 'a';
		  $content = qq`$content</query></question><question id="$BOOK_ID_SHORT-ch$CHAPTER-$id-quesma" numbered="false" title="" number=""><query id="$BOOK_ID_SHORT-ch$CHAPTER-$id-quesma-qry">MA:_NL_`;
		}
	  }
	  $content = qq`<p id="ch${CHAPTER}-keyterms">Match the term to its definition by clicking the term first, then the definition.</p><question id="$BOOK_ID_SHORT-ch$CHAPTER-1-quesma" numbered="false" title="" number=""><query id="$BOOK_ID_SHORT-ch$CHAPTER-1-quesma-qry">MA:_NL_$content</query></question>`;
	}

    # Force browsers to line break correctly at dashes.
    # NOTE: This is still experimental.  Please let Bruce know if it
    # causes any problems.
    $content = fix_linebreaks($content);
	
	# DETERMINE IF REAL LEVEL 1
	if ($args =~ /level="1"/) {
		#if ($args !~ /block_type="key_terms"/ && $args !~ /block_type="CT_questions"/ && $args !~ /block_type="GI_questions"/ && $args !~ /block_type="for realzies"/ && $args !~ /block_type="orange_bar"/ && $level1 ne "true") {
		if ($args !~ /block_type="/ && $level1 ne "true"  &&  $args !~ /chapter="fm"/) {
			$args =~ s`level="1"`level="2" block_type="level_1"`g;
		}
	}
	$level1 = "false";
	if ($content =~ /block_type="remember"/) {
	  $level1 = "true";
	}
	
	

    return qq`<section ${args}>${content}</section>`;
}
sub process_term {
	my $keyterm = shift;
	my $lckeyterm = lc $keyterm;
	$lckeyterm =~ s|&#\d+;| |g;
	$lckeyterm =~ s| \([^)]+\)||g;
	$lckeyterm =~ s|<.+?>||g;
	$lckeyterm =~ s`[^A-Za-z0-9]` `g;
	my $def = $terms{$lckeyterm};
	$keyterm = "$letter.$keyterm::${def}_NL_";
	$letter = ++$letter;
    return $keyterm;
}


sub process_figure {
    my $args = shift; # attributes on <figure> tag
    my $content = shift; # contents of figure
	my $figure; # figure name
	my $figureID; # figure number
	
	# convert chapopen.jpg to chapopen.png
	# $content =~ s`puls3e_chapopen(\d+).jpg`puls3e_chapopen\1.png`;
	
	# REALLY?
	$content =~ s`<phrase block_type="fig-photo"><strong>((?:(?!</strong>).)*)</strong></phrase>`<phrase block_type="fig_title">\1</phrase>`g;
	
	# DOUBLE REALLY?
	$content =~ s`block_type="fig-photo">`block_type="fig_title">`g;
	
	# TRIPLE REALLY?
	$content =~ s`block_type="fig-title">`block_type="fig_title">`g;
	
	$content =~ s` \[((?:(?!\]).)*)\]</caption>` <asset_source>[\1]</asset_source></caption>`g;
	$content =~ s` <phrase block_type="small">\[((?:(?!\]).)*)\]</phrase></caption>` <asset_source>[\1]</asset_source></caption>`g;
	
	$content =~ s`(Chapter (\d\d?))`'<phrase block_type="section-link"><link href="pulsipherconcepts3e_ch' . sprintf("%02d", $2) . '_1.html" target="_self">' . $1 . '</link></phrase>'`ge; ##FORCE 2 digits
	
	# add title to chapopen
	if ($content =~ /chapopen/) {
		$args .= qq` block_type="chapopen"`;
		my $chTitle = ${CHAPTER_TITLE};
		$chTitle =~ s`Chapter (\d+) `chapter <phrase block_type="chnum">\1</phrase></p><p>`g;
		#$content = qq`<box block_type="orange"><p block_type="grey">${chTitle}<phrase block_type="arrow">&#9654;</phrase></p></box>${content}`;
		$content = qq`${content}<caption><p block_type="grey">${chTitle}<phrase block_type="arrow">&#9654;</phrase></p></caption>`;
	}
	
	# create weird Photo Essay layout
	if ($content =~ /PHOTO ESSAY:/) {
		$args .= qq` block_type="photo-essay"`;
		if ($content =~ /(puls3e_ch(\d+)_fig_(\d+)).jpg/) {
		    $figure = "$1";
			$figureID = "$2.$3";
		}
		$content =~ s`</phrase><br/>`</phrase> `g;
		$content =~ s`</caption> ?<asset_source>((?:(?!</asset_source>).)*)</asset_source>`<asset_source>\1</asset_source></caption>`g;
		$content =~ s`<image src="([^"]+)"/><caption>((?:(?!</caption>).)*)</caption>`<caption>\2</caption><image src="\1"/>`g;
		$content =~ s`<caption><phrase block_type="fig_title">PHOTO ESSAY: ((?:(?!</phrase>).)*)</phrase> ?`<caption><phrase block_type="fig_id"><phrase block_type="fig-num"><link href="asset/img_ch${CHAPTER}/${figure}.html" target="_pop">FIGURE ${figureID}</link> <phrase block_type="spacer"></phrase> PHOTO ESSAY: </phrase><phrase block_type="fig_title">\1</phrase></phrase>`g;
		$content =~ s`FIGURE </link>`</link>`g;
	}
	
	# create Think Geo layout
	if ($args =~ /think/) {
		$content =~ s`</caption> ?<asset_source>((?:(?!</asset_source>).)*)</asset_source>`<asset_source>\1</asset_source></caption>`g;
		$content =~ s`<image src="([^"]+)"/><caption>((?:(?!</caption>).)*)</caption>`<caption>\2</caption><image src="\1"/>`g;
	}
	
	# create LOCAL LIVES
	if ($content =~ /LOCAL LIVES/) {
		$args .= qq` block_type="local-lives"`;
		$content =~ s`x-large`large`g;
		$content =~ s`</phrase><br/>`</phrase> `g;
		$content =~ s`<image src="([^"]+)"/><caption>((?:(?!</caption>).)*)</caption>`<caption>\2</caption><image src="\1"/>`g;  # Put caption on top
		if ($content !~ /<strong>/) {
		  $content =~ s`</phrase> ((?:(?!</caption>).)*)</caption>`</phrase> <strong>\1</strong></caption>`g;  # add <strong> tag
		}
	}
	
	# Create supp html files for figures
    my ($img_file_no_ext) = $content =~ /src="(?:[^"]+\/)?(.*?)\.(?:jpg|gif|png|tiff)"/;
	
	if ($content =~ /_tab_(\d\d?)/) {
	    $args .= qq` block_type="table"`;
		$content =~ s`<image src="([^"]+)"/><caption><strong>TABLE (\d\d?).(\d\d?) ((?:(?!</caption>).)*)</caption>`<caption><strong><phrase block_type="table-link"><link href="asset/img_ch\2/${BOOK_ID_SHORT}_ch\2_table_\3.html" target="_pop">TABLE \2.\3</link></phrase>\4</caption><image src="\1"/>`g;
		create_supp_file("figure", $img_file_no_ext, $args, $content);
		$img_file_no_ext =~ s`_tab_`_table_`g;
	}

    create_supp_file("figure", $img_file_no_ext, $args, $content, "");
	
    return qq`<figure ${args}>${content}</figure>`;
}



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

	if ($args =~ /block_type="numbered/) {
		$args .= qq` type="ordered"`;
	} else {
		$args .= qq` type="unordered"`;
	}


    # 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
	my $tablenotes1 = shift;
	my $tablenotes2 = shift;
	my $tablenotes3 = shift;
	my $tablenotes4 = shift;
	my $tablenotes5 = shift;
	my $tablenotes = "${tablenotes1}${tablenotes2}${tablenotes3}${tablenotes4}${tablenotes5}";

    # process each <td>
    $content =~ s`(<td[^>]*>.*?</td>)`process_td($1)`ge;
	
	# move caption to top
	if ($content =~ /<table_caption>TABLE \d+\.\d+/) {
		$args .= qq` caption-placement="Top"`;
    }

    # Create table supp html files for numbered tables.  You may need to adjust
    # the regex for your particular xml.
    if ($content =~ /<table_caption>TABLE \d+\.(\d+)/) {
		my $table_file_no_ext = "${BOOK_ID_SHORT}_ch${CHAPTER}_table_$1";
		$content =~ s`<table_caption>TABLE (\d\d?).(\d\d?):? ((?:(?!</table_caption>).)*)</table_caption>`<table_caption><phrase block_type="table-link"><link href="asset/img_ch\1/${BOOK_ID_SHORT}_ch\1_table_\2.html" target="_pop">TABLE \1.\2</link></phrase>\3</table_caption>`g;
	    create_supp_file("table", $table_file_no_ext, $args, $content, $tablenotes);
    }

	$content =~ s`(Chapter (\d\d?))`'<phrase block_type="section-link"><link href="pulsipherconcepts3e_ch' . sprintf("%02d", $2) . '_1.html" target="_self">' . $1 . '</link></phrase>'`ge; ##FORCE 2 digits
	
    # yes, we are returning "tabler"
    return qq`<tabler ${args}>${content}</tabler>${tablenotes}`;
}


sub process_td {
    my $td = shift;


    return $td;
}


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

	# !puls3e_geo!
	if ($args =~ /![pP]uls3e_geo! GEOGRAPHIC INSIGHTS/) {
		$args  =~ s`![pP]uls3e_geo! ``g;
		$args  =~ s`block_type="bx_v3"`block_type="geo-insights"`g;
    }
	if ($args =~ /![pP]uls3e_geo\d\d?! GEOGRAPHIC INSIGHT \d\d?/) {
		$args  =~ s`![pP]uls3e_geo\d\d?! ``g;
		$args  =~ s`block_type="bx_v3"`block_type="geo-insight-single"`g;
    }
	if ($args =~ /![pP]uls3e_hand! THINGS TO REMEMBER/) {
		$args  =~ s`![pP]uls3e_hand! ``g;
		$args  =~ s`block_type="bx_v[56]"`block_type="remember"`g;
    }
	if ($args =~ /!hand! THINGS TO REMEMBER/) {
		$args  =~ s`!hand! ``g;
		$args  =~ s`block_type="bx_v[56]"`block_type="remember"`g;
    }
	if ($args =~ /![pP]uls3e_sun! ON THE BRIGHT SIDE/) {
		$args  =~ s`![pP]uls3e_sun! ``g;
		$args  =~ s`block_type="bx_v[35]"`block_type="bright-side"`g;
    }
	if ($content =~ /&#62; / || $args =~ /&#62; /) {
		$args  =~ s`block_type="bx_v3"`block_type="bx_v62"`g;
		$args  =~ s`&#62; ``g;
		$content  =~ s`&#62; ``g;
    }

	if ($args =~ /PE-questions/) {
		$content  =~ s`<p id="([^"]+)" block_type="txt">((?:(?!</p>).)*)</p>`<question id="${1}"><query id="${1}-query">ES: ${2}</query></question>`g;
    }

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


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

    # in-text refs to numbered figures pop
	$content =~ s`(Figure (\d\d?).(\d\d?)[A-Z]?(, [A-Z])?(&#8211;[A-Z])?)`<phrase block_type="figlink"><link href="asset/img_ch\2/${BOOK_ID_SHORT}_ch\2_fig_\3.html" target="_pop">\1</link></phrase>`g;
	$content =~ s`(Table (\d\d?).(\d\d?))`<phrase block_type="figlink"><link href="asset/img_ch\2/${BOOK_ID_SHORT}_ch\2_table_\3.html" target="_pop">\1</link></phrase>`g;
	#$content =~ s`<phrase block_type="fig">(\(?Table (\d\d?).(\d\d?)\)?)</phrase>`<phrase block_type="fig"><link href="asset/img_ch\2/${BOOK_ID_SHORT}_ch\2_table_\3.html" target="_pop">\1</link></phrase>`g;
	$content =~ s`<strong>(\(?Table (\d\d?).(\d\d?)\)?)</strong>`<strong><link href="asset/img_ch\2/${BOOK_ID_SHORT}_ch\2_table_\3.html" target="_pop">\1</link></strong>`g;
	
	$content =~ s`(Chapter (\d\d?))`'<phrase block_type="section-link"><link href="pulsipherconcepts3e_ch' . sprintf("%02d", $2) . '_1.html" target="_self">' . $1 . '</link></phrase>'`ge; ##FORCE 2 digits
	
	# Add Video Link from videoLink.pl file
	if ($content =~ /<image src="asset\/pics\/video\.jpg"\/>(.+)/) {
		my $vidTitle = $1;
		my $ucVidTitle = uc $vidTitle;
		$ucVidTitle =~ s`<STRONG>``g;
		$ucVidTitle =~ s`</STRONG>``g;
		$ucVidTitle =~ s`[^A-Z ]``g;
		$ucVidTitle =~ s/^\s+|\s+$//g; #trim
		$ucVidTitle =~ s`\s+` `g; #remove double spaces
		if(exists($videoLinks{$ucVidTitle})) {
			if ($videoLinks{$ucVidTitle} eq "cut") {
				$content =~ s`(<image src="asset\/pics\/video\.jpg"\/>.+)``g;
			} else {
				$content =~ s`(<image src="asset\/pics\/video\.jpg"\/>.+)`<phrase block_type=\"vidLink\"><link href=\"$videoLinks{$ucVidTitle}.html\" target=\"_pop\">\1</link></phrase>`g;
			}
		} else {
			print STDERR "$ucVidTitle\n";
		}
	}
	
	#add termref
	$content =~ s`<phrase block_type="term">((?:(?!</phrase>).)*)</phrase>`process_termref($1)`ge;

	if ($content eq "") {
		return '';
	} else {
		return qq`<p ${args}>$content</p>`;
	}
}

sub process_termref {
    my $text = shift;
    my $lctext = lc $text;
	$lctext =~ s|&#\d+;| |g;
	$lctext =~ s| \([^)]+\)||g;
	$lctext =~ s|<.+?>||g;
	$lctext =~ s`[^A-Za-z0-9]` `g;
   # if(exists($terms{$lctext})) {
	  $text = '<termref term="' . $lctext . '">' . $text . '</termref>';
	#}
    return $text;
}

# 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
	my $tablenotes = shift; # the tablenotes 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";
		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";
	    }
	}
	else {
	    $write_file = $filename;
	}
    }
    else {
	print STDERR "Bad type ($type) passed to create_supp_file\n";
	return;
    }

    $content =~ s`<link (href="[^"]+")>((?:(?!</link>).)*)</link>`<span data-type="link" data-$1>$2</span>`g;
	$tablenotes =~ s`<link (href="[^"]+")>((?:(?!</link>).)*)</link>`<span data-type="link" data-$1>$2</span>`g;
	#$content =~ s`<caption>(.*?)</caption>`<span data-type="caption">$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="([^"]+)"/;
	
	my $figNum = "";
	if ($content =~ /puls3e_ch${CHAPTER}_fig_(\d\d?).jpg/) {
	    $figNum = $1;
	}
	
	#puls3e_ch6_fig_5.jpg
	
	$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>`g;

	# We assume there is only one <caption> per <figure>
	if ($content =~ /<caption>(.*?)<\/caption>/) {
	    #print STDERR "Figure ${CHAPTER}.${figNum}\n";
	} else {
		print STDERR "Figure ${CHAPTER}.${figNum}\n$content\n\n";
	}
	$content =~ s`<caption>(.*?)</caption>`<div data-type="figure_text"><span data-type="caption"><span data-type="number">Figure ${CHAPTER}.${figNum}: </span>\1</span></div>\n`;

	$content =~ s`</figure>`</div>\n`;
	$content =~ s`<layout [^>]+>``;

	$content =~ s`src="(?:[^"]+\/)?(.*\.(?:jpg|gif|png|tiff))"`src="$1"`;

	$content =~ s`<image `<img `;

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

	$supp_content =~ s`visual_history`visual_history_pop`;
	
    }
    elsif ($type eq "table") {

	my ($block_type) = $args =~ /block_type="([^"]+)"/;
	my ($id) = $args =~ /id="([^"]+)"/;
	my ($layout) = $content =~ /<layout(?:[^>]+)?align="([^"]+)"(?:[^>]+)?\/>/;
	my ($width) = $content =~ /<layout(?:[^>]+)?width="([^"]+)"(?:[^>]+)?\/>/;
	my ($border) = $content =~ /<layout(?:[^>]+)?border="([^"]+)"(?:[^>]+)?\/>/;
	my ($number) = $args =~ /number="([^"]+)"/;

	$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=""><table width="auto" height="auto">${content}</table></div>${tablenotes}`;
    }

    $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 $chnum = sprintf("%02d",$CHAPTER);

    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${chnum}.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">
    //<!--

\$(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()
