MATLAB syntax highlighting for google-code-prettify
An implementation of MATLAB syntax highlighting for google-code-prettify.
The following constructs are recognized:
% comment
and %{
,%}
)'hello world'
)1
, -2.5
, 1i
, 2.9E-5
, etc…)!touch filename
)...
)x'
and x.'
)>> now
)??? Error in ...
and Warning: ...
)()
, []
, {}
)<>=~@&;,:!-+*^.|\/
)if
, else
, end
, etc…)inf
, nan
, varargin
, etc..)The “full” version also adds highlighting for MATLAB functions:
cos
, plot
, etc…)(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.
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
Version | Source |
---|---|
Full | ![]() |
Lite | ![]() |
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>
For instructions on installing userscripts for various browsers, see
this page. Also check out the wiki for sample screenshots.
Apply MATLAB syntax highlighting on Stack Overflow and other
Stack Exchange sites. The script is only activated on questions tagged asmatlab
.
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.
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.
Apply MATLAB syntax highlighting on MATLAB Answers.
Apply MATLAB syntax highlighting on File Exchange.
Apply MATLAB syntax highlighting on MATLAB Examples.
Project released under the MIT License.