Immprep v1.09 - c2023..2026 EJN
  process files to simplify the use of immersive reader in Edge for TTS purposes

Latest update:
  v1.09 - added -markdown option, see 

Processing .html files:
  immprep -html <file> [-noimmersive] [-emdash] [-cleanup] [-edge <path>] [-trigger "<text>"]

  1. copy source .html file to immprep.html
  2. process immprep.html
  3. launch edge (optional)
  4. this option uses a FILE, not a PATH, as opposed to -gdocs and -word

Processing Google Docs .zip files:
  immprep -gdocs <path> [-noimmersive] [-emdash] [-anytime] [-cleanup] [-unread] [-edge <path>] [-trigger "<text>"]

  1. look for latest zip file in specified folder
  2. unpack first *.html file from .zip to immprep.html
  3. process immprep.html
  4. clean up (remove zip, optional, careful with -anytime!!)
  5. launch edge (optional)

Processing Word .docx files:
  immprep -word <path> [-pandoc <path>] [-noimmersive] [-emdash] [-anytime] [-cleanup] [-edge <path>] [-trigger "<text>"] [-markdown]

  1. pandoc.exe must exist and be found, otherwise this will fail
  2. look for newest *.docx file in specified folder (consider using anytime)
  3. convert file to immprep.html.tmp using pandoc.exe
  4. process immprep.html
  5. launch edge (optional)

Options & Parameters (either use all with, or all without a preceeding dash):
  -help             - shows this text, aborts any other command line processing
  -version          - shows more detailed version information
  -html <file>      - html source, process specified html file
  -word <path>      - word source, process newest .docx file in specific path, requires pandoc.exe
  -gdocs <path>     - gdocs source, process first .html in newest .zip file in specific path
  -noimmersive      - don't look for <immersive> markers (see below)
  -emdash           - any -- will be converted to &mdash; (not with markdown)
  -pandoc <path>    - specify path to pandoc.exe, default is in the same folder as imprep.exe
  -pandoc ~default~ - assume pandoc.exe in in the same folder as immprep.exe
  -edge <path+file> - triggers the use of edge and specifies path to edge
  -edge ~default~   - default is "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
  -anytime          - don't care how old the zip / word file is (otherwise it needs to be younger than 10 seconds)
  -cleanup          - remove old .zip file and other remains
  -unread           - will allow cleanup even if nothing was sent to edge (careful when combining with anytime!)
  -mode <n>         - defines how the source file is processed and result is created (not yet implemented)
  -trigger "<text>" - immprep will strip everyting up to the specified text, then start reading at that point
  -nowait           - ignore errors, just exit
  -wait             - always wait before exiting (wait > nowait)
  -html4            - tells pandoc to generate html4 instead of default html5
  -keephash         - will leave a hash in the text, instead of replacing it with ... break ... (not with markdown)
  -markdown         - uses markdown and text as intermediate formats, instead of HTML

Note on -markdown:
  The -markdown option will generate a .txt file in the markdown format, then modify that file for
  a better result in Edge. The on-screen output in Edge will look better without the -markdown option,
  but sometimes Microsoft's servers misbehave a bit and then -markdown might work better.
  -markdown does not support -trigger, so you must use << immersive >> and << endimmersive >>
  It will currently only work for docx sources, i.e. only for the combo -word <source.docx> -markdown

Note on options ('option flag enforced' error message):
  - if any option is preceeded by a dash, then ALL options need to be preceeded by a dash!
  - immprep version gdocs ~downloads~ mdash     - works
  - immprep version gdocs ~downloads~ -mdash    - will cause an error
  - immprep -version -gdocs ~downloads~ -mdash  - works
  - immprep -version -gdocs ~downloads~ mdash   - will cause an error

Replacements for <file> and <path>:
  ~currentfolder~ - replaced with current directory
  ~downloads~     - replaced with c:\users\username\downloads
  ~nearby~        - replaced with folder containing immprep.exe
  ~programfolder~ - same as ~nearby~
  ~tempfolder~    - replaced with temp folder
  ~default~       - default paths hardcoded in emmprep, may differ from yours, handle with care

Immersive:
  - when processing a file immprep looks for the specific trigger or the following keywords:
  - <immersive> <<immersive>> < immersive > << immersive >> mark the start of a section to be read
  - <endimmersive> and its variants mark the end of a section to be read.
  - you can use multiple <immersive> and <endimmersive> tags
  - detection of these tags suppressed by specifying the noimmersive parameter

Mode:
  0 - default
  x - not yet implemented

Return values:
  0 - Edge was opened
  1 - Edge was not opened

Call made to pandoc.exe using docx to html (default):
  pandoc -w -s --metadata title=immprep -o <inputfile> -f docx <outputfile>

Call made to pandoc.exe using docx to html (generates markdown):
  pandoc -w -s --metadata title=immprep -o <inputfile> <outputfile>

Errors:
  - pandoc throwing a weird error with Word as source, or imprep halting completely
      1. exit Word
      2. exit Edge
      3. exit Imprep
      4. check the folder with your source document
      5. delete leftover .htm, .tmp, and .html files
      6. restart Word
      7. change one letter in your document and save it
      8. retry
  - immprep opening the wrong document with Word as a source
      1. change one letter in your document and save it
      2. retry 
  - Microsoft Edge sometimes stops
      1. make sure you don't use the -html4 flag
      2. sometimes the Microsoft TTS servers are buggy, in that case use the -markup option

Examples:
  immprep html test.html noimmersive mdash
  immprep html "test.html" trigger "chapter 1 - "
  immprep gdocs ~downloads~ cleanup mdash edge ~default~
  immprep word d:\documents\novel\ anytime edge "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" 

Example batch file for Google Docs (called via TouchPortal with Google Docs in focus):
  rem tp_immersive_gdocs.bat
  immprep -gdocs ~downloads~ -cleanup -mdash -edge ~default~
  if errorlevel 1 goto noedge
    nircmd wait 500
    nircmd sendkeypress ctrl+shift+u
  :noedge
  exit

TouchPortal script for Google Docs:
  Key Press Alt+Shift+F
  Wait for 500 msec
  Key Press Alt+Shift+D
  Wait for 500 msec
  Key Press Alt+Shift+H
  Run script & open tp_immersive_gdocs.bat

Example batch file for Word (called via TouchPortal with Word in focus):
  rem tp_immersive_word.bat
  immprep -word d:\novel\ -mdash -edge ~default~ -anytime
  if errorlevel 1 goto noedge
    nircmd wait 500
    nircmd sendkeypress ctrl+shift+u
  :noedge
  exit

TouchPortal script for Word:
  Key Press Alt+F
  Wait for 500 msec
  Key Press S
  Wait for 500 msec
  Run script & open tp_immersive_word.bat

Old style batch file:
  cd c:\users\username\downloads
  waitfile "zipfilename.zip" 4 exist
  If errorlevel 2 goto done
  If errorlevel 1 goto done
  :zipfound
    tar -xf "zipfilename.zip"
    immprep -html "htmlfilename.hmtl" -mdash
    del "zipfilename.zip"
    "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" "c:\users\username\downloads\htmlfilename.html"
    waitwindow "htmlfilename*" 4
    timeout 1
    nircmd sendkeypress ctrl+shift+u
  :done
  exit

Suggested tools:
  edge           - Microsoft Edge is required for TTS
  nircmd         - Nirsoft multi-tool, here used to send a keypress to Microsoft Edge
  pandoc         - required when processing word .docx files
  touch portal   - use an old phone or tablet as your stream deck / macro keyboard
  tar            - Windows build-in unpacker (you already have this one)
  waitfile.exe   - halts batchfiles until a file (does not) exist (for more complex batch files)
  waitwindow.exe - halts batchfiles until a window is (not) open (for more complex batch files)
  cmdow          - allows manipulation of windows - note: once focus has been set using /ACT it cannot be changed again!

See also:
  the blog post at the following link shows how to use immprep, Touch Portal and nircmd to automate TTS proofreads:
  https://ninelizardsblog.blogspot.com/2023/03/launch-applications-from-touch-portal.html

Summary:
  immprep -html|-gdocs|-word <path|path+file> [-pandoc <path>] [-noimmersive] [-emdash] [-anytime] [-cleanup] [-unread] [-mode <n>] [-edge <path+file>|~default~] [-trigger "<text>"] [-wait|-nowait] [-markdown]
  with <path> = c:\|c:\path\abc.def|~nearby~|~current~|~downloads~|~default~|~tempfolder~
