home *** CD-ROM | disk | FTP | other *** search
- /* Convert `*xyz*' i.e. bold emphasis to `jmsempth{xyz}' */
- /* and {\tt{}"} pairs to `` and '' */
- /* both of these functions are to aid conversion of html to TeX files */
- /* Written by David Fong 9th December 1996 */
- /* Version 1.0 30th December 1996 */
- parse arg filenames
- parse var filenames infile ' ' outfile
-
- say 'HTML to TeX aid'
- say "Convert `*xyz*' i.e. bold emphasis to `jmsempth{xyz}'"
- say "and {\tt{}\" || '22'x || "} pairs to `` and ''"
- say "both of these functions are to aid conversion of html to TeX files"
- say "Written by David Fong 9th December 1996"
- say ''
-
- n_strings=2 /* this can be modified, but the number of data sets
- below then needs to be modified as well */
-
- /* data sets */
-
- find_string.1='*'
- repl_strng1.1='\jmsempth{'
- repl_strng2.1='}'
-
- flag.1=false
- start_line.1=0
- end_line.1=0
- start_pos.1=0
- end_pos.1=0
-
- find_string.2='{\tt{}\"}'
- repl_strng1.2="``"
- repl_strng2.2="''"
-
- flag.2=false
-
- start_line.2=0 /* line of first quote marker */
- end_line.2=0 /* line of second quote marker */
- start_pos.2=0
- end_pos=0
-
- /* start of program proper */
-
- line_count=0 /* number of lines read */
- written_line=0 /* number of lines written */
-
- if arg() ~= 1 | outfile == '' | infile == '' then do
- say 'Incorrect number of arguments'
- say 'Usage : HTMLtoTeXAid infile outfile'
- exit
- end
-
- if open('in',infile,'r') then do
- if open('out',outfile,'w') then do
- do while ~eof('in')
- line_count=line_count+1 /* current line being read */
- a.line_count=readln('in')
- do i=1 to n_strings
- pos.i=0
- still_search.i=true
- end /* prime these markers to start of string */
- do until sum_flag=false
- do i=1 to n_strings
- if still_search.i=true then do
- pos.i=index(a.line_count,find_string.i,pos.i+1)
- if pos.i>0 then
- call handle_marker(i)
- else
- still_search.i=false
- end
- end
-
- sum_flag=false
- do i=1 to n_strings /* loop until we find no more markers */
- if pos.i>0 then
- sum_flag=true
- end
- end
- call output_lines() /* write as many lines as we can */
- end
- if line_count>written_line then do /* output the rest of the lines, we have stopped reading */
- do i=written_line+1 to line_count
- writeln('out',a.i)
- a.i=''
- end
- end
- close('out')
- end
- else
- say 'Could not open "'outfile'" for writing.'
- close('in')
- end
- else
- say 'Could not open "'infile'" for reading.'
-
- say ''
- exit
-
- output_lines: procedure expose a. line_count n_strings flag. start_line. written_line
-
- write_to_line=line_count
-
- do i=1 to n_strings /* write up to the line which we are still working on! */
- if flag.i=true & (write_to_line>=start_line.i) then
- write_to_line=start_line.i-1
- end
-
- if write_to_line>written_line then do
- do i=written_line+1 to write_to_line
- writeln('out',a.i)
- a.i=''
- end
- written_line=write_to_line
- end
-
- return
-
- handle_marker: procedure expose flag. start_pos. pos. end_pos. start_line. line_count a. n_strings find_string. repl_strng1. repl_strng2.
-
- s=arg(1) /* which strings are we replacing? */
-
- if flag.s=false then do
- flag.s=true
- start_pos.s=pos.s
- start_line.s=line_count
- end
- else do
- flag.s=false
- end_pos.s=pos.s
- do i=start_line.s to line_count
- writech('STDOUT',' '||i||' ') /* print out the line numbers and text to be `converted' */
- if start_line.s=line_count then /* both markers on the same line */
- say left(delstr(a.i,1,start_pos.s-1),end_pos.s+length(find_string.s)-start_pos.s)
- else if i=start_line.s then /* first line */
- say delstr(a.i,1,start_pos.s-1)
- else if i=line_count then /* last line */
- say left(a.i,end_pos.s+length(find_string.s)-1)
- else
- say a.i
- end
- writech('STDOUT','Convert?:')
- pull response
- if length(response)>0 then
- response=left(upper(response),1)
- if (response=='') | (response=="Y") then do /* `no' response is taken as yes */
- zz=start_line.s
- a.zz=delstr(a.zz,start_pos.s,length(find_string.s))
- a.zz=insert(repl_strng1.s,a.zz,start_pos.s-1)
- call adjust_markers1(s) /* marker positions may have just been changed */
- /* end_pos.s may have been changed by previous line */
- a.line_count=delstr(a.line_count,end_pos.s,length(find_string.s))
- a.line_count=insert(repl_strng2.s,a.line_count,end_pos.s-1)
- call adjust_markers2(s) /* marker positions may have just been changed again! */
- do i=start_line.s to line_count
- say '-->'||a.i
- end
- end
- else do
- /* a difficult situation to handle */
- /* what does the user actually mean, to ignore both markers or just the first one? */
- /* to increase flexibility, we take it to mean to ignore the first one */
- flag.s=true
- start_pos.s=end_pos.s
- start_line.s=line_count
- end
- end
- return
-
- adjust_markers1: procedure expose pos. start_pos. line_count n_strings start_line. end_pos. repl_strng1. find_string.
-
- s=arg(1)
-
- do i=1 to n_strings
- if start_line.s=start_line.i then do
- if start_pos.i>start_pos.s then
- start_pos.i=start_pos.i+length(repl_strng1.s)-length(find_string.s)
- /* all markers so far found have either been stored as start_pos etc.,
- or have already been used to `close' their expression,
- with the exception of the marker just found (see below) */
- if pos.i>start_pos.s then
- pos.i=pos.i+length(repl_strng1.s)-length(find_string.s)
- end
- end
- if start_line.s=line_count then /* handle the marker just found */
- end_pos.s=end_pos.s+length(repl_strng1.s)-length(find_string.s)
-
- return
-
- adjust_markers2: procedure expose pos. n_strings start_pos. line_count start_line. start_pos. repl_strng2. find_string.
-
- s=arg(1)
-
- do i=1 to n_strings
- if line_count=start_line.i then do
- if start_pos.i>end_pos.s then
- start_pos.i=start_pos.i+length(repl_strng2.s)-length(find_string.s)
- if pos.i>end_pos.s then
- pos.i=pos.i+length(repl_strng2.s)-length(find_string.s)
- end
- end
-
- return
-
-