
// PandocTest.java
// Andrew Davison, ad@coe.psu.ac.th, Nov. 2022

/* Pandoc can convert a very wide range of file formats
   into others. The tests here are restricted to converting
   markdown text into MS Word DOCX or Powerpoint PPTX.

   Pandoc: https://pandoc.org/
   https://pandoc.org/MANUAL.html

   Markdown Info
    * https://www.w3schools.io/file/markdown-introduction/
    * https://agea.github.io/tutorial.md/
    * https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
    * https://stackabuse.com/markdown-by-example/

    Usage:
      > compile PandocTest.java
      > run PandocTest
*/

import java.io.*;

import org.zeroturnaround.exec.*;


public class PandocTest
{

  public static void main(String[] args)
  {
    if ( ZUtils.cmdExit("pandoc", "-v") == 0)
      System.out.println("Pandoc installed");
    else {
      System.out.println("Pandoc not installed");
      return;
    }

    String fnm = "md-syntax.md";
     // https://github.com/examples-hub/markdown-examples
    System.out.println("Converting " + fnm + " to DOCX");
    ZUtils.cmdOut("pandoc", fnm, "-o", ZUtils.stripExt(fnm)+".docx");

    fnm = "conrad.md";  // Powerpoint found a problem with content
                        // click ok and then save the file
     // https://stymied.medium.com/what-slides-from-markdown-5239ed31e7ac
    System.out.println("Converting " + fnm + " to PPTX");
    ZUtils.cmdOut("pandoc", fnm, "-o", ZUtils.stripExt(fnm)+".pptx");

    String[] markdown1 = { 
       "# Markdown",
       "Markdown is intended to be as easy-to-read and easy-to-write as is feasible.",
       "Readability, however, is emphasized above all else. A Markdown-formatted document should be publishable as-is, as plain text, without looking like it's been marked up with tags or formatting instructions. While Markdown's syntax has been influenced by several existing text-to-HTML filters, the single biggest source of inspiration for Markdown's syntax is the format of plain text email."
    };
    convertMD(markdown1, "article.docx");

    String[] markdown2 = { 
       "## Getting up",
         "- Turn off alarm",
         "- Get out of bed",
       "## Breakfast",
         "- Eat eggs",
         "- Drink coffee",
       "## Dinner",
         "- Eat spaghetti",
         "- Drink wine",
       "![picture of a bench](bench.jpg)"
     };
    convertMD(markdown2, "slides.pptx");
  }  // end of main()


  // -------------------- utils -----------------------


  private static void convertMD(String[] markdown, String fnm)
  /* The markdown text is saved to a temporary file,
     which is passed to pandoc for conversion to the
     specified format based on the extension of the supplied
     filename.
  */
  {
    for (int i=0; i < markdown.length; i++)
       markdown[i] = markdown[i] + "\n"; 
             // so lines are treated as separate

    String mkFnm = ZUtils.writeToTemp(markdown);
    int res = ZUtils.cmdExit("pandoc", mkFnm, "-o", fnm);
    if (res == 0)
      System.out.println("Markdown converted and saved to " + fnm);
    else
      System.out.println("Conversion failed (" +
                           exitMeaning(res) + "); " + fnm + " not saved");
  }  // end of convertMD()


  private static String exitMeaning(int v)
  // from https://pandoc.org/MANUAL.html
  {
    switch(v) {
      case 1 : return "IO Error";
      case 3 : return "Fail-On Warning Error";
      case 4 : return "App Error";
      case 5 : return "Template Error";
      case 6 : return "Option Error";
      case 21: return "Unknown Reader";
      case 22: return "Unknown Writer";
      case 23: return "Unsupported Extension";
      case 24: return "Citeproc Error";
      case 25: return "Bibliography Error";
      case 31: return "Epub Subdirectory Error";
      case 43: return "PDF Error";
      case 44: return "XML Error";
      case 47: return "PDF Program Not Found";
      case 61: return "Http Error";
      case 62: return "Should Never Happen";
      case 63: return "Some Error";
      case 64: return "Parse Error";
      case 65: return "Parsec Error";
      case 66: return "MakePDF Error";
      case 67: return "Syntax Map Error";
      case 83: return "Filter Error";
      case 84: return "Lua Error";
      case 91: return "Macro Loop";
      case 92: return "UTF8 Decoding Error";
      case 93: return "Ipynb Decoding Error";
      case 94: return "Unsupported Charset";
      case 97: return "Could Not Find Data File";
      case 98: return "Could Not Find Metadata File";
      case 99: return "Resource Not Found";
      default: return "Unknown Exit Code " + v;
    }
  }  // exitMeaning()


}  // end of PandocTest class