How to add meta tags to HTML export?

Amazed by the websites generated by TeXmacs, I’m trying to use TeXmacs to generate my own website. I’ve figured out how to customize the translation of visible tags, but I’m also thinking about adding meta information via the <meta> tag, e.g. metadata added to the document via Document -> Metadata... and <meta name="viewport" content="width=device-width, initial-scale=1"> to make the site responsive.

Is there an existing way of achieving this?


Does not seems so. The relevant code to convert TeXmacs documents to HTML is in $TEXMACS_PATH/progs/convert/html/tmhtml.scm, in particular the procedure tmhtml-file is responsible to produce the top level HTML document structure, there you can see that it gathers various META definitions to put into HEAD, things which happens here:

       (h:title ,@(tmhtml title))
       (h:meta (@ (name "generator")
		  (content ,(string-append "TeXmacs " (texmacs-version)))))
      (h:body ,@body))

where all the other meta were collected in the list xhead. Unfortunately I do not see a way to programmatically add more. But you can modify this function to recognize other tags in the TeXmacs document and produce the relevant meta definitions. Look for example how the html-head-javascript tag is handled there:

    (if (with-extract doc "html-head-javascript")
	    (let* ((code (with-extract doc "html-head-javascript"))
	       (script `(h:script (@ (language "javascript")) ,code)))
	    (set! xhead (append xhead (list script)))))

This looks for html-head-javascript in the TeXmacs document doc and if found it extract its value in the variable code and create a new HTML node <script....> using the form (h:script ...), save it in the variable script and then it appends it to the end of the xhead list, which then it is placed in the right place in the output HTML document by the first snipped above. You just need to add a similar statement to handle whatever tag you would like to put there.

This will handle the low level part. If you want also to have a UI dialog to specify these details, it could be done but would require still some more code. If you want to look how to implement dialogs look for open-document-metadata and document-metadata-editor in $TEXMACS_PATH/progs/generic/document-widgets.scm. The document metadata dialog is defined by the following code

(tm-widget ((document-metadata-editor u) quit)
    (refreshable "document-metadata"
        (item (text "Title:")
          (input (initial-set u "global-title" answer) "string"
                 (list (buffer-get-metadata u "title")) "30em"))
        (item (text "Author:")
          (input (initial-set u "global-author" answer) "string"
                 (list (buffer-get-metadata u "author")) "30em"))
        (item (text "Subject:")
          (input (initial-set u "global-subject" answer) "string"
                 (list (buffer-get-metadata u "subject")) "30em"))))
         (initial-default u "global-title" "global-author" "global-subject")
         (refresh-now "document-metadata"))
        // //
        ("Ok" (quit))))))

it uses variables contained in the environment tree of the document (which I do not know much about) to store the values. This is another mechanism you could use to propagate used defined values, but is not what I was mentioning above.


Thank you for so detailed explanation! I’ll try it some time this week.

I still have a (maybe silly) question: Should I modify $TEXMACS_PATH/progs/convert/html/tmhtml.scm directly or use some rewriting mechanism? Is it generally recommended to modify the files in $TEXMACS_PATH?

No, they will be rewritten with new versions of TeXmacs, unless you use some tricks. But there is a better way: if you put your custom version in $TEXMACS_HOME_PATH/progs/convert/html/tmhtml.scm, it should override the default version, since $TEXMACS_HOME_PATH is (usually) searched over before $TEXMACS_PATH for any resource. Alternatively if you only what to customise a single procedure which has been tm-defined somewhere in the base source code, you can just go on an redefine it (always with tm-define) in scheme and your version takes over. This is one of the design goals of tm-define, to be able to let the user customise any internals of TeXmacs to suits her/his needs.
In the particular case we are talking about I guess it is better just to have your own version of the file since you will have to modify more than one procedure and some of them are just defined, so are local to the module and inaccessible from outside.