MATLAB Answers

0

Is it possible to use relative links to files in Live Editor?

Asked by Martin Roempert on 11 Aug 2017
Latest activity Answered by Mikhail
on 10 Oct 2018
If I export a hyperlink created in Live Editor *.mlx file to HTML I simply want a
<a href = "doc.html">
but I always get some
<a href = "http://doc.html">
or
<a href = "file://doc.html">
and I do not want to have absolute pathes in the generated HTML.
By the way ... if I set the Target URL to
D:\doc.html
I get the expected result in the generated HTML.
<a href = "file:///D:/doc.html">
But this is not what I want.

  0 Comments

Sign in to comment.

Products

2 Answers

Answer by Baptiste Ottino on 11 Aug 2017
Edited by Baptiste Ottino on 11 Aug 2017
 Accepted Answer

Yes, this is pretty annoying. I found a work-around using the live editor markup. Instead of using the "hyperlink" button, write the link in the following syntax in your mlx file:
[the label of the link](doc.html)
If you write the right-hand bracket last, the label will highlight automatically in blue just like a normal hyperlink would, but you won't have the annoying http: formatting.
EDIT
I realize that I didn't really answered your initial question. The goal of my first answer was to let you have the correct links in your mlx files. Now let's see how to make it work in html.
I work with R2016b, and I get the same issue when I use the "export as html" button. I will need a bit more thought to find a proper fix, but here are two ways of fixing this:
First solution (dirty hotfix)
Overload the function
% for R2016b
matlab.internal.richeditor.openAndConvert
% for 2017a
matlab.internal.liveeditor.openAndConvert
or just modify it if you have admin tools, so that when you publish through the button, those lines are executed:
% Construct the name of your html-file from your mlx-file
[~, mlxName, mlxExt] = fileparts(mlxFile);
filePath = strcat(mlxName, '.html');
% Read the html file
fin = fopen(filePath, 'r');
text = fread(fin, '*char')';
fclose(fin);
% delete bad URLs paths
text=strrep(text,'https://localhost:61415/toolbox/matlab/codetools/liveeditor/','');
% Write back the html file
fout = fopen(filePath, 'w');
fprintf(fout, '%s', text);
fclose(fout);
It is a bit of a brute force solution, but it works.
Second solution (for best output)
Personally, I have been very disappointed by the way the live editor publishes html. For one thing, it does not let you choose your xsl stylesheet, so you're stucke with the default. It also plots your equations and plots in bitmap and not in vectorial.
I have been working on a script that fixes these issues. Unfortunately, I am still waiting for my compagny to give me the green light for publication on mathworks, since it was developped during work hours. I'll give you the general idea though: save the live script as a m-file, and publish to html using the publish function.
function mlx2html(mlxFile)
% Construct the name of your m-file from your mlx-file
% (slightly different name to prevent shadowing)
[~, mlxName, mlxExt] = fileparts(mlxFile);
mFile = strcat(mlxName,'_ouput', '.m');
% Convert mlx-file to m-file
% for R2016b
matlab.internal.richeditor.openAndConvert(mlxFile, mFile);
% for 2017a
% matlab.internal.liveeditor.openAndConvert(mlxFile, mFile);
% Publish the m-file in html
publish(mFile)
end
Use this solution if you úse this publish function to have good looking html pages. If I do post my code, I will be sure to comment here. Best of luck!

  3 Comments

Hello Baptiste,
thank you for answering!
First of all the link works perfectly from within Live Editor now. Unfortunately if I enter the markup as you proposed it get the following in HTML.
https://localhost:61415/toolbox/matlab/codetools/liveeditor/doc.html
I tested with MATLAB R2016b and R2017a.
Do you have any idea what might be wrong?
Regards
Martin
Hello Martin,
Unfortunately, I don't know of an easy fix. I edited my first answer to give you some advice on how to proceed.
Hello Baptiste,
thank you for updating your answer. I use a slightly modified version of your First Solution. Converting from *.mlx to *.m was destroying some formulas. In addition to your URL replacement I added a replacement for the style section, that helps me to have the same look and feel as I would use the publish function and *.m files with something like that:
% <style>
% h2 {font-size:2em;color:C45400;font-weight:normal;}
% h3 {font-size:1.25em;}
% a {color:#187DBB;}
% p {font-size:1.1em;color:#404040;}
% </style>
I am looking forward to seeing your script published.
Regards
Martin

Sign in to comment.


Answer by Mikhail
on 10 Oct 2018

Looks like export to HTML doesn't do things to relative links anymore as of R2018b.

  0 Comments

Sign in to comment.