Search code examples
htmltexinfo

texinfo include HTML header from file


I am writing a Texinfo manual, and for its HTML I need to include the contents of another file into the <head> ... </head> section of the HTML output. To be more specific, I want to add mathjax capability to the HTML version of the output to show equations nicely. But I can't seem to find how I can add its <script>...</script> to the header!


Solution

  • Since I couldn't find an answer and doing the job my self didn't seem to hard, I wrote a tiny C program to do the job for me. It did the job perfectly in my case!

    Ofcourse, if there is an option in Texinfo that does the job, that would be a proper answer, this is just a remedy to get things temporarily going for my self.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define ADDTOHEADER "             \n\
    <script type=\"text/javascript\"  \n\
      src=\"http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\">\n\
    </head>"
    
    
    void
    addtexttohtml(char *filename)
    {
      char toadd[]=ADDTOHEADER;
      size_t len=0;
      ssize_t read;
      FILE *in, *out;
      char tmpname[]="tmp457204598345.html", *line=NULL;
    
      in=fopen(filename, "r");
      out=fopen(tmpname, "w");
      if (in == NULL) exit(EXIT_FAILURE);
      if (out == NULL) exit(EXIT_FAILURE);
    
      while ((read = getline(&line, &len, in)) != -1)
        {
          if(strcmp(line, "</head>\n")==0) break;
          fprintf(out, "%s", line);
        }
    
      fprintf(out, "%s", toadd);
    
      while ((read = getline(&line, &len, in)) != -1)
        fprintf(out, "%s", line);
    
      if(line)
        free(line);
    
      fclose(in);
      fclose(out);
    
      rename(tmpname, filename);
    }
    
    int
    main(int argc, char *argv[])
    {
      int i;
    
      for(i=1;i<argc;i++)
        addtexttohtml(argv[i]);
    
      return 0;
    }
    

    This program can easily be compiled with $ gcc addtoheader.c.

    Then we can easily put the compiled program (by default it should be called a.out) with the HTML files and run:

    $ a.out *.html
    

    You can just change the macro for any text you want.