Syntax Highlighting MATLAB code in forum posts?

Discourse can syntax highlight several different coding languages (it uses highlightjs to do so) which can be specified if you use the backtick blocks, however it isn’t syntax highlight MATLAB syntax yet, so I was wondering if you could try to enable it in the discourse settings panel (there is an option for this apparently)?

% this is a test of MATLAB syntax highlighting, only generic things like strings 
% and numbers are highlighted, this comment is not, suggesting wrong language used
if a > b
    c = unint32(12);
else
    c = 'Char array';
end
# Ruby code for comparison, note comments are coloured etc.
class Greeter
  def initialize(name)
    @name = name.capitalize
  end

  def salute
    puts "Hello #{@name}!"
  end
end

# Create a new object
g = Greeter.new("world")

# Output "Hello World!"
g.salute

For testing, the code from here:

n = 20; % number of points
points = [random('unid', 100, n, 1), random('unid', 100, n, 1)];
len = zeros(1, n - 1);
points = sortrows(points);
%% Initial set of points
plot(points(:,1),points(:,2));
for i = 1: n-1
    len(i) = points(i + 1, 1) - points(i, 1);
end
while(max(len) > 2 * min(len))
    [d, i] = max(len);
    k = on_margin(points, i, d, -1);
    m = on_margin(points, i + 1, d, 1);
    xm = 0; ym = 0;
%% New point
    if(i == 1 || i + 1 == n)
        xm = mean(points([i,i+1],1))
        ym = mean(points([i,i+1],2))
    else
        [xm, ym] = dlg1(points([k, i, i + 1, m], 1), ...
            points([k, i, i + 1, m], 2))
    end

    points = [ points(1:i, :); [xm, ym]; points(i + 1:end, :)];
end

%{
    This is a block comment. Please ignore me.
%}

function [net] = get_fit_network(inputs, targets)
    % Create Network
    numHiddenNeurons = 20;  % Adjust as desired
    net = newfit(inputs,targets,numHiddenNeurons);
    net.trainParam.goal = 0.01;
    net.trainParam.epochs = 1000;
    % Train and Apply Network
    [net,tr] = train(net,inputs,targets);
end

foo_matrix = [1, 2, 3; 4, 5, 6]''';
foo_cell = {1, 2, 3; 4, 5, 6}''.'.';

cell2flatten = {1,2,3,4,5};
flattenedcell = cat(1, cell2flatten{:});

Ok, can’t say the highlighting is particularly informative, but it works (i’ve changed the setting). Thanks for the report!

1 Like

Thanks Dee, possibly another highlighting style would be a bit clearer, but I think that is good enough!