Skip to content
MATLAB syntax highlighting for google-code-prettify
JavaScript HTML CSS
Latest commit c7a4c9a Jun 5, 2016 @amroamroamro bump to node.js 4 as minimum version
this requirement comes from jsdom >= 7.x
Failed to load latest commit information.
demo
dist
src
test
.editorconfig
.eslintrc.json
.gitignore
.jscsrc
.jshintrc
.travis.yml
Gruntfile.js
LICENSE
README.md
appveyor.yml
package.json

README.md

MATLAB syntax highlighting for Google Code Prettify

Build Status Build Status devDependency Status Built with Grunt License

An implementation of MATLAB syntax highlighting for google-code-prettify.

The following constructs are recognized:

  • single line and block comments (% comment and %{,%})
  • quoted string ('hello world')
  • number literals (1, -2.5, 1i, 2.9E-5, etc...)
  • shell escape (!touch filename)
  • line continuation (...)
  • transpose operator (x' and x.')
  • command prompt (>> 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..)
  • user-defined indentifiers (function and variable names not matched in previous steps)

The "full" version also adds highlighting for MATLAB functions:

(Since this increases the resulting script size, a separate "lite" version is also available which excludes function names detection).

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

Build

Requirements: Node.js and the NPM package manager.

To rebuild the project, run the following in the command line:

$ cd prettify-matlab
$ npm install
$ npm run build

# (optional) run unit tests
$ npm test

First this will install build dependencies, then generate the output javascript and stylesheet files in the dist directory using the source templates from the src directory.

dist/
|
|-- js/
|    |-- full/
|    |    |-- lang-matlab.js      # code-prettify extension (full version)
|    |    |-- lang-matlab.min.js  # and minified version
|    |
|    |-- lite/
|         |-- lang-matlab.js      # code-prettify extension (lite version)
|         |-- lang-matlab.min.js  # and minified version
|
|-- css/
|    |-- matlab.css               # optional skin for code-prettify
|    |-- matlab.min.css           # and minified version
|
|-- userscripts/
     |-- prettify-matlab.user.js                  # userscripts for
     |-- switch-lang.user.js                      #  Stack Overflow
     |-- prettify-mathworks-answers.user.js       # userscripts for
     |-- prettify-mathworks-fileexchange.user.js  #  various MathWorks
     |-- prettify-mathworks-examples.user.js      #  websites

Usage: Extension

Version Source
Full Source
Lite Source

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 code-prettify project documentation). Next include the MATLAB language extension, and place your source code inside a preformatted HTML tag as follows:

<html>
<head>
<title>MATLAB</title>
<link rel="stylesheet" type="text/css" href="prettify.css" />
<script src="prettify.js"></script>
<script src="lang-matlab.js"></script>
</head>

<body onload="prettyPrint();">
<pre class="prettyprint lang-matlab">
% example code
x = [1, 2, 3]';
fprintf('sum(x^2) = %f\n', sum(x.^2));
</pre>
</body>
</html>

When PR.prettyPrint() is called, marked sections will be pretty-printed, and the default styles will be applied. You can customize them with your own, or use the provided stylesheet which has a color scheme inspired by that of the MATLAB IDE (with some modifications).

See the demo page for a demonstration.

UPDATE: The MATLAB extension is now integrated upstream in google/code-prettify. So you can use the auto-loader to directly load both code-prettify along with the MATLAB extension via one URL:

<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?lang=matlab"></script>

Usage: Userscripts

For instructions on installing userscripts for various browsers, see this page. Also check out the wiki for sample screenshots.

Stack Overflow

Apply MATLAB syntax highlighting on Stack Overflow and other Stack Exchange sites. The script is only activated on questions tagged as matlab.

Source Install

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.

Source Install

UPDATE: Stack Overflow recently integrated MATLAB syntax support, so you should get proper syntax highlighting by default. This userscript is still useful if you want to get full highlighting for function names as well as the customized stylesheet which applies colors resembling the MATLAB editor.

MathWorks MATLAB Answers

Apply MATLAB syntax highlighting on MATLAB Answers.

Source Install

MathWorks File Exchange

Apply MATLAB syntax highlighting on File Exchange.

Source Install

MathWorks MATLAB Examples

Apply MATLAB syntax highlighting on MATLAB Examples.

Source Install

License

Project released under the MIT License.

Something went wrong with that request. Please try again.