MATLink icon indicating copy to clipboard operation
MATLink copied to clipboard

help command returns HTML in MEvaluate

Open szhorvat opened this issue 12 years ago • 12 comments

Since changing MEvaluate to use evalc, the help command returns HTML.

Example:

In[15]:= MEvaluate["help pi"]

Out[15]= " <strong>pi</strong>     3.1415926535897....
    <strong>pi</strong> = 4*atan(1) = imag(log(-1)) = 3.1415926535897....

    Reference page in Help browser
       <a href=\"matlab:doc pi\">doc pi</a>

"

This can be reproduced in MATLAB by running it as matlab -nodesktop then comparing help pi with evalc('help pi')

szhorvat avatar Apr 11 '13 14:04 szhorvat

We have two options here:

  • try to get MATLAB not to return HTML (my preference)
  • render the HTML

The MATLAB GUI does the latter:

Screen Shot 2013-04-11 at 11 00 29

szhorvat avatar Apr 11 '13 15:04 szhorvat

Related info:

  • http://www.mathworks.com/matlabcentral/newsreader/view_thread/94396
  • http://blogs.mathworks.com/community/2008/01/21/marking-up-m-with-html/

szhorvat avatar Apr 11 '13 15:04 szhorvat

It's not just for help; HTML strings are returned for most errors that include the function name (with a link to the help docs or to the file itself, if it's a custom file). I've reverted MEvaluate, so this issue isn't there right now, but since the plan is to eventually use evalc for MEX, this needs to be figured out.

rsmenon avatar Apr 11 '13 21:04 rsmenon

There is an undocumented way to explicitly turn off hyperlinks and other HTML goodies:

feature('hotlinks','off')

This value is queried internally by help to decide whether to add HTML tags to the output.

Note: I did not test the above..

amroamroamro avatar May 05 '13 05:05 amroamroamro

Wow, this is very useful information (which support did not give me). It does indeed work, but I need to use 1 and 0 instead of 'off'. However, it doesn't seem to be possible to easily turn it off for good inside evalc. It is possible to turn it off for a single evalc call only though. Here's a command line transcript showing the behaviour:

>> feature('hotlinks')

ans =

     0

>> evalc('feature(''hotlinks'')')

ans =


ans =

     1



>> evalc('feature(''hotlinks'',0)')

ans =

     ''


>> evalc('feature(''hotlinks'')')  

ans =


ans =

     1



>> evalc('feature(''hotlinks'',0); feature(''hotlinks'')')

ans =


ans =

     0



>> 

Since we're already wrapping everything MEvaluateed with some extra code to catch errors, we might as well add feature('hotlinks',0) at the beginning.

szhorvat avatar May 05 '13 14:05 szhorvat

@amroamroamro Thanks a lot! That's very useful and saves us the trouble of having to strip HTML tags from the output (which is always messy).

rsmenon avatar May 05 '13 14:05 rsmenon

@szhorvat: hmm you are right, for some reason the value seems to be reset inside evalc... Fortunately this only affect help output, error messages do not contain "jump to line" links, even when using evalc.

Try replacing help with the lower level builtin function helpfunc

@rsmenon: come to think of it, stripping tags might not be so bad here. I know one should never parse HTML using regexp, but the set of HTML that is outputted is fairly simple of the form:

<tag attrib="val">text</tag>

Should be easy to strip HTML and extract the text.

amroamroamro avatar May 05 '13 16:05 amroamroamro

Not exactly related to the issue, but you mentioned wrapping evaluated strings in extra code to catch errors.

Now I haven't looked at the source code yet, but let me share another less-known trick; eval and evalc take a second input that is executed only when the evaluation errors in the first:

isError = false;
evalc('nonexistent', 'isError = true;')

maybe this pattern could be used in your code..

amroamroamro avatar May 05 '13 16:05 amroamroamro

@amroamroamro Thanks! This does look very useful indeed. This will all become very relevant when we switch to the MEX interface (instead of the Engine interface). Perhaps we could have a chat sometime about how we're planning to do this. I'd love to listen to your opinion! (freenode or SO chat or something else)

szhorvat avatar May 05 '13 16:05 szhorvat

@szhorvat sure i just joined ##matlab on freenode. you'll find me on the top the list :)

amroamroamro avatar May 05 '13 17:05 amroamroamro

With work having started on the MEX version, this issue is now more serious. It's most annoying in error messages.

Observation:

Right now we use getReport() to extract the message string. If MATLAB is run as matlab -nodesktop, it getReport() won't return any HTML in the message string.

However, help still does.

szhorvat avatar Jun 12 '14 14:06 szhorvat

getReport() has an explicit option to turn off hyperlinks, regardless of how MATLAB was started:

try
    %...
catch ME
    errmsg = getReport(ME, 'extended', 'hyperlinks','off');
    disp(errmsg)
end

amroamroamro avatar Jun 13 '14 08:06 amroamroamro