| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # tohtml.sh [v. 0.2.01, reldate: 04/13/12, a teeny bit less buggy] |
| 3 | |
| 4 | # Convert a text file to HTML format. |
| 5 | # Author: Mendel Cooper |
| 6 | # License: GPL3 |
| 7 | # Usage: sh tohtml.sh < textfile > htmlfile |
| 8 | # Script can easily be modified to accept source and target filenames. |
| 9 | |
| 10 | # Assumptions: |
| 11 | # 1) Paragraphs in (target) text file are separated by a blank line. |
| 12 | # 2) Jpeg images (*.jpg) are located in "images" subdirectory. |
| 13 | # In the target file, the image names are enclosed in square brackets, |
| 14 | # for example, [image01.jpg]. |
| 15 | # 3) Emphasized (italic) phrases begin with a space+underscore |
| 16 | #+ or the first character on the line is an underscore, |
| 17 | #+ and end with an underscore+space or underscore+end-of-line. |
| 18 | |
| 19 | |
| 20 | # Settings |
| 21 | FNTSIZE=2 # Small-medium font size |
| 22 | IMGDIR="images" # Image directory |
| 23 | # Headers |
| 24 | HDR01='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">' |
| 25 | HDR02='<!-- Converted to HTML by ***tohtml.sh*** script -->' |
| 26 | HDR03='<!-- script author: M. Leo Cooper <[email protected]> -->' |
| 27 | HDR10='<html>' |
| 28 | HDR11='<head>' |
| 29 | HDR11a='</head>' |
| 30 | HDR12a='<title>' |
| 31 | HDR12b='</title>' |
| 32 | HDR121='<META NAME="GENERATOR" CONTENT="tohtml.sh script">' |
| 33 | HDR13='<body bgcolor="#dddddd">' # Change background color to suit. |
| 34 | HDR14a='<font size=' |
| 35 | HDR14b='>' |
| 36 | # Footers |
| 37 | FTR10='</body>' |
| 38 | FTR11='</html>' |
| 39 | # Tags |
| 40 | BOLD="<b>" |
| 41 | CENTER="<center>" |
| 42 | END_CENTER="</center>" |
| 43 | LF="<br>" |
| 44 | |
| 45 | |
| 46 | write_headersscip-shell shell abs-guide 10-4 write_headers(). () |
| 47 | { |
| 48 | echoscip-shell system . . echo. "$HDR01" |
| 49 | echoscip-shell system . . echo. |
| 50 | echoscip-shell system . . echo. "$HDR02" |
| 51 | echoscip-shell system . . echo. "$HDR03" |
| 52 | echoscip-shell system . . echo. |
| 53 | echoscip-shell system . . echo. |
| 54 | echoscip-shell system . . echo. "$HDR10" |
| 55 | echoscip-shell system . . echo. "$HDR11" |
| 56 | echoscip-shell system . . echo. "$HDR121" |
| 57 | echoscip-shell system . . echo. "$HDR11a" |
| 58 | echoscip-shell system . . echo. "$HDR13" |
| 59 | echoscip-shell system . . echo. |
| 60 | echoscip-shell system . . echo. -n "$HDR14a" |
| 61 | echoscip-shell system . . echo. -n "$FNTSIZE" |
| 62 | echoscip-shell system . . echo. "$HDR14b" |
| 63 | echoscip-shell system . . echo. |
| 64 | echoscip-shell system . . echo. "$BOLD" # Everything in bold (more easily readable). |
| 65 | } |
| 66 | |
| 67 | |
| 68 | process_textscip-shell shell abs-guide 10-4 process_text(). () |
| 69 | { |
| 70 | while read line # Read one line at a time. |
| 71 | do |
| 72 | { |
| 73 | if [scip-shell system . . `[`. ! "$line" ] # Blank line? |
| 74 | then # Then new paragraph must follow. |
| 75 | echoscip-shell system . . echo. |
| 76 | echoscip-shell system . . echo. "$LF" # Insert two <br> tags. |
| 77 | echoscip-shell system . . echo. "$LF" |
| 78 | echoscip-shell system . . echo. |
| 79 | continue # Skip the underscore test. |
| 80 | else # Otherwise . . . |
| 81 | |
| 82 | if [[ "$line" =~ \[*jpg\] ]] # Is a graphic? |
| 83 | then # Strip away brackets. |
| 84 | temp=$( echoscip-shell system . . echo. "$line" | sedscip-shell system . . sed. -e 's/\[//' -e 's/\]//' ) |
| 85 | line=""$CENTER" <img src="\"$IMGDIR"/$temp\"> "$END_CENTER" " |
| 86 | # Add image tag. |
| 87 | # And, center it. |
| 88 | fi |
| 89 | |
| 90 | fi |
| 91 | |
| 92 | |
| 93 | echoscip-shell system . . echo. "$line" | grepscip-shell system . . grep. -q _ |
| 94 | if [scip-shell system . . `[`. "$?" -eq 0 ] # If line contains underscore ... |
| 95 | then |
| 96 | # =================================================== |
| 97 | # Convert underscored phrase to italics. |
| 98 | temp=$( echoscip-shell system . . echo. "$line" | |
| 99 | sedscip-shell system . . sed. -e 's/ _/ <i>/' -e 's/_/<\/i> /' | |
| 100 | sedscip-shell system . . sed. -e 's/^_/<i>/' -e 's/_/<\/i>/' ) |
| 101 | # Process only underscores prefixed by space, |
| 102 | #+ or at beginning or end of line. |
| 103 | # Do not convert underscores embedded within a word! |
| 104 | line="$temp" |
| 105 | # Slows script execution. Can be optimized? |
| 106 | # =================================================== |
| 107 | fi |
| 108 | |
| 109 | |
| 110 | |
| 111 | # echo |
| 112 | echoscip-shell system . . echo. "$line" |
| 113 | # echo |
| 114 | # Don't want extra blank lines in generated text! |
| 115 | } # End while |
| 116 | done |
| 117 | } # End process_text () |
| 118 | |
| 119 | |
| 120 | write_footersscip-shell shell abs-guide 10-4 write_footers(). () # Termination tags. |
| 121 | { |
| 122 | echoscip-shell system . . echo. "$FTR10" |
| 123 | echoscip-shell system . . echo. "$FTR11" |
| 124 | } |
| 125 | |
| 126 | |
| 127 | # main () { |
| 128 | # ========= |
| 129 | write_headersscip-shell shell abs-guide 10-4 write_headers(). |
| 130 | process_textscip-shell shell abs-guide 10-4 process_text(). |
| 131 | write_footersscip-shell shell abs-guide 10-4 write_footers(). |
| 132 | # ========= |
| 133 | # } |
| 134 | |
| 135 | exit $? |
| 136 | |
| 137 | # Exercises: |
| 138 | # --------- |
| 139 | # 1) Fixup: Check for closing underscore before a comma or period. |
| 140 | # 2) Add a test for the presence of a closing underscore |
| 141 | #+ in phrases to be italicized. |