2

I'm trying to get *.htm files to open in a different application than for *.html (MIME Type text/html), as generally described in GNOME Help > Add a custom MIME type for all users. I've created /usr/share/mime/packages/htm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="application/x-html-notepad">
    <sub-class-of type="text/html"/>
    <comment>HTML Notepad</comment>
    <glob pattern="*.htm"/>
  </mime-type>
</mime-info>

and updated the desktop database as root (update-mime-database /usr/share/mime). It still shows the built in HTML mime type for an HTML file with the *.htm extension, as if the above definition had no effect (removing the <sub-class-of> makes no difference):

$ gio info saved-web-page.htm | grep standard::content-type
  standard::content-type: text/html

Even when there's no HTML in the file it ignores the custom MIME type definition:

$ touch empty-file.htm
$ gio info empty-file.htm | grep standard::content-type
  standard::content-type: text/plain

Please, what have I missed? I'm on GNOME 3.28.2 / Ubuntu 18.04.

EDIT: I've since phrased the question more fundamentally here on a different forum: MIME type text/html: how & where defined?

  • 1
    Could it be that "*.htm" is already defined in freedesktop.org.xml, and your definition doesn't override that one? – Ray Butterworth Aug 2 '19 at 12:54
  • It could be; I'd never heard of that file before. On my system /usr/share/mime/packages/freedesktop.org.xml is over 40K lines and definition <mime-type type="text/html"> begins on line 33910. Is this file or format spec documented anywhere, or should I get reverse engineering? Or can a local copy of that definition (e.g. ~/.local/share/mime/packages/html+htm.xml) override the global one in the MIME cache, with 2 separate definitions for each <glob> pattern? – rphair Aug 2 '19 at 17:03
  • So here's the best description of what's in that file (freedesktop.org/wiki/Specifications/shared-mime-info-spec), which includes no practical information about how to alter the industry standard definitions on an individual system (the link at the top of my OP only addresses new MIME types). Hence the reverse engineering: instead, I'd like to know how, or whether, such an override would work before I start coding. Even a partial answer about overriding would be a valid answer to this question. – rphair Aug 2 '19 at 17:16
0

As established in comments to the OP, the text/html MIME type is defined in /usr/share/mime/packages/freedesktop.org.xml. This can be overridden by a user by placing a new definition of text/html in the local MIME folder ~/.local/share/mime/packages.

This MIME type may be applied to the two target file extensions because of these lines of XML in the file above:

  <mime-type type="text/html">
    ...
    <glob pattern="*.html"/>
    <glob pattern="*.htm"/>
  </mime-type>

So I've been able to split the two MIME types according to extension by creating a file ~/.local/share/mime/packages/html-split.xml looking like this:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
  <mime-type type="text/html">
    <comment>HTML document</comment>
    ... INCLUDING EVERYTHING UP UNTIL <glob> FROM THE OLD DEFINITION ...
    <glob pattern="*.html"/>
  </mime-type>
  <mime-type type="application/x-html-notepad">
    <sub-class-of type="text/html"/>
    <comment>HTML Notepad</comment>
    <glob pattern="*.htm"/>
  </mime-type>
</mime-info>

What I've NOT tested: how pattern matches in the <magic> section remaining in the XML definition for text/html might prevent MIME type application/x-html-notepad from being applied to an *.htm file. So comments about the <magic> tag, especially its priority= attribute, will be welcome here.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.