24 September, 2013

MATLAB Highlighter

The MATLAB Highlighter userscript

The MATLAB Highlighter userscript was created to let the users to view the .m files in the File Exchange section with syntax highlightning on the MathWorks portal.

The script was tested in Firefox and Chrome. Firefox users can use this script by the Greasemonkey userscript handler, while Chrome users can activate it by using the Tampermonkey extension. You have to simply download the following file, and add its contents as an userscript.

Version 0.98

The MATLAB Highlighter JavaScript library

The MATLAB Highlighter Javascript library was designed to be a simple and lightweight syntax colorizer function. There are lots of great projects already on this topic such as rainbow.js or highlighter.js, but I wanted a small library designed especially for MATLAB, doing only the minimal necessary steps. That is the reason, why MATLAB Highlighter was created.

What does it look like?

By using this small library you can turn your code below:

function displaySubsets(A)
   bitNo = length(A);         % number of bits
   setNo = 2 ^ bitNo - 1;     % number of sets
   for setId = 0 : setNo
      % convert number to a binary string and that to logical indices
      setIndices = logical(dec2bin(setId, bitNo) - '0')
      % select the current set by using the logical indices
      currentSet = A(setIndices)
   end
end

Into this:

function displaySubsets(A)
   bitNo = length(A);         % number of bits
   setNo = 2 ^ bitNo - 1;     % number of sets
   for setId = 0 : setNo
      % convert number to a binary string and that to logical indices
      setIndices = logical(dec2bin(setId, bitNo) - '0')
      % select the current set by using the logical indices
      currentSet = A(setIndices)
   end
end

How shall I use it? 

There will be multiple versions of varying capabilities in the future to meet everyones needs. Each version of the MATLAB Highlighter consists of two files: a JS file containing the program and a CSS file defining the colours and styles to be applied.

After downloading the files of the appropriate version include the files into your webpage source. Below is a sample, but you shall to change the file names to fulfil your parameters: 

<link href="/matlab-highlighter.css" rel="stylesheet" type="text/css">
<script src="/matlab-highlighter.min.js"></script>

In the following paragraphs a full usage example will be given.

Planned improvements

  • Line numbers and line highlighting
  • Possibility of highlighting built-in MATLAB functions too
  • Turn the project to be open source

Versions

Version 1.55

Fourth, enhanced version with more options. Change notes:

  • More settings possible to find both <pre> and <code> elements.
  • Custom class-name can be given.

Download

Another style created by Cristian Escudero is available at the following location:

Documentation

The main function is highlightMATLABCode() which can be called in three different ways:

  1. highlightMATLABCode()
    • If no parameter is given, all <pre> elements having class matlab-code are processed.

  2. highlightMATLABCode(id)
    • The object having the given id will be processed.

  3. highlightMATLABCode({tagCode: true, tagPre: true, className: 'matlab-code'})
    • Three parameters can be set:
      • tagCode can be true or false. If true, <code> elements will be processed.
      • tagPre can be true or false. If true, <pre> elements will be processed.
      • className can be an empty string '' or a given class name, as in the example above matlab-code is given. If this parameter is not an empty string, only the elements having given class name will be processed.

Examples

Select all <code> elements. The class does not matter, since it is an empty string:

highlightMATLABCode({tagCode: true, tagPre: false, className: ''});

Select all <pre> elements, which have class matlab-code:

highlightMATLABCode({tagCode: false, tagPre: true, className: 'matlab-code'});

Select all <code> and <pre> elements having class name my-code:

highlightMATLABCode({tagCode: true, tagPre: true, className: 'my-code'});

Version 1.32 deprecated

This was the third release providing more options on selecting the elements being processed. It lacked of appropriate string handling in comments, so this major bug was fixed in Version 1.55, see the detailed documentation there.

 Version 1.27

Second release with enhanced capabilities. Change notes:

  • Some minor bugs removed.
  • Better IE support.
  • Better string support: identificating transposing operator.
  • Bracket identification added.

Download

Documentation

The main function is highlightMATLABCode(id) to be called with an optional parameter. If an id is defined, it processes only the webpage element having the given id. When no parameter is passed, the function loops through all <pre> elements in the webpage and handles them. It is important, that each <pre> element is processed only when it has class matlab-code to avoid mixing up another preformatted texts.

Version 0.98 deprecated

This was the first release with minimal capabilities. It handled the keywords, numbers, comments and strings in the code and colorized them. 

Usage example

A full usage example is here:

<!DOCTYPE HTML>
<html>
<head>
  <link href="/matlab-highlighter-1.32.css" rel="stylesheet" type="text/css">
  <script src="/matlab-highlighter-1.32.min.js"></script>
</head>
<body onload="highlightMATLABCode();">
<pre class="matlab-code">
% Multiplies a number by two.
function twice(A)
  return A * 2;
end
</pre>
<pre>
This is a preformatted text.
</pre>
</body>
</html>

The output is:

% Multiplies a number by two.
function twice(A)
  return A * 2;
end
This is a preformatted text.

The first element is coloured while the second is not. The highlighter code is running after the page has been loaded: the colorizer function is attached to the onload event of the page.

Reviews

We have included MATLAB Highlighter in our website: Image Processing Blog, and it works and looks great. Now, the published code looks much better and it is easier to follow. Thank you!

Elad Eizner – Image Processing Blog

Contact

Any questions, bug reports, modification requests are welcome on zoltan [at] matlabtricks [dot] com.

License

The MATLAB Highlighter is licensed under the MIT license. 

Copyright (c) 2013, Zoltán Fegyver

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

         

New comment

comments powered by Disqus