Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
MATLAB syntax highlighting for google-code-prettify
JavaScript Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
css
demo
js
src
.gitignore
README.markdown
Rakefile

README.markdown

MATLAB syntax highlighting for Google Code prettify

This script provides MATLAB syntax highlighting for the google-code-prettify project. Intended to be used on Stack Overflow and other Stack Exchange sites.

The project was inspired by the similar Mathematica.SE syntax highlighter.

Currently the following constructs are recognized:

  • single line as well as block comments (% this is a comment)
  • quoted string ('hello world')
  • number literals (1, -2.5, 1i, 2.9E-5, etc...)
  • system commands (!touch filename)
  • line continuation characters (...)
  • transpose operator (single quote) vs. strings
  • command prompt/command output (>> now)
  • error/warning messages (??? Error in ... and Warning: ...)
  • parentheses, brackets, braces ((), [], {})
  • other operators (<>=~@&;,:!-+*^.|\/)
  • MATLAB language keywords (if, else, end, etc...)
  • some special variables and constants (inf, nan, varargin, etc..)
  • over 1300 builtin functions from core MATLAB (cos, plot, etc...)
  • additional functions from popular toolboxes (Statistics, IPT, and Optimization)
  • user-defined indentifiers (function and variable names not matched in previous steps)

See the wiki for sample screenshots.

BUILDING

You can rebuild the project by running rake SO:build. This will create the output javascript files in the js directory using the templates sources from src. (This step requires Rake, a Make-like build tool for Ruby, used here to provide basic template processing).

Stack Overflow

To obtain MATLAB syntax highlighting on Stack Overflow, install the userscript with your preferred browser (see this page for some instructions).

The script is only activated on questions tagged matlab.

In addition, a separate userscript is included to allow switching the language used by the prettifier. It adds a small button to the top-right corner of each code block, with an attached drop-down menu to allow language selection.

MathWorks (MATLAB Answers / File Exchange)

Similarly, you can apply the syntax hightighting on both MATLAB Answers and File Exchange websites from MathWorks. Simply install the corresponding userscript for Answers or File Exchange websittes.

google-code-prettify extension

To apply the MALTAB syntax highlighting on code snippets in your own web pages, first include the prettify scripts and stylesheets in your document (as explained in the prettify project documentation). To use the MATLAB language extension, include the MATLAB script, and place your source code inside a preformatted HTML tag as follows:

<pre class="prettyprint lang-matlab">
    <code>
    % example code
    x = [1,2,3];
    sum(x.^2)
    </code>
</pre>

Upon calling prettyPrint(), marked sections will be pretty-printed, and the default styles will be applied. You can customize them with your own, or use the provided CSS file which resembles the color scheme of the MATLAB IDE (with some additions of my own).

Check the demo page for a demonstration.

MIT LICENSE

Copyright (c) 2013 by Amro <amroamroamro@gmail.com>

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.

Something went wrong with that request. Please try again.