Перейти к содержанию
    

Раскраска кода

Локально прикрутил раскраску с помощью greasemonkey, google prettifier и следующего скрипта

// ==UserScript==
// @name        Elecronix code prettifier
// @match *://electronix.ru/forum/*
// @version     1
// @grant       none
// ==/UserScript==

// google prettify highlighting script
var HLJS = "//cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?lang=matlab&skin=sunburst";

(function() {
  'use strict';

  // Don't run inside iframes (Looking at you GreaseMonkey + TinyMCE)
  if (window.top !== window.self) return; 

  // Runs a function in the document. Basically like a Content Script.
  // http://wiki.greasespot.net/Content_Script_Injection
  function contentEval(source) {

    // Check for function input.
    if ('function' == typeof source) {
      // Execute this function with no arguments, by adding parentheses.
      // One set around the function, required for valid syntax, and a
      // second empty set calls the surrounded function.
      source = '(' + source + ')();'
    }

    // Create a script node holding this  source code.
    var script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.textContent = source;

    // Insert the script node into the page, so it will run, and immediately
    // remove it to clean up.
    document.body.appendChild(script);
    //document.body.removeChild(script);
  }


  // Given a src attribute, makes a <script> tag into the end of <body>
  function contentScript(source) {
    var tag = document.createElement('script');
    tag.setAttribute('type', 'application/javascript');
    tag.src = source;

    document.body.appendChild(tag);
    //document.body.removeChild(tag);
    return tag;
  }

  var ds = document.getElementsByClassName('codemain');

  while(ds.length)    
  {
     var old_elem = ds[0]; 
     var new_elem = document.createElement('pre');
     new_elem.innerHTML = old_elem.innerHTML;    
     new_elem.className = 'prettyprint';
     old_elem.parentNode.replaceChild(new_elem, old_elem);    
  }
  
  var hl = contentScript(HLJS);

})();

 

Выглядит как-то так:

 

pic1.jpg

 

Матлаб иногда глючит:

 

pic2.jpg

 

как переключать скины написано тут:

https://github.com/google/code-prettify/blo...ting_started.md

 

Если есть спецы по js, то буду рад любым замечаниям по улучшению.

Поделиться сообщением


Ссылка на сообщение
Поделиться на другие сайты

Нельзя ли прикрутить раскраску кода для matlab/c++/c ?

В смысле "раскраску"?

Синтаксическую подсветку что ли?

Поделиться сообщением


Ссылка на сообщение
Поделиться на другие сайты

Присоединяйтесь к обсуждению

Вы можете написать сейчас и зарегистрироваться позже. Если у вас есть аккаунт, авторизуйтесь, чтобы опубликовать от имени своего аккаунта.

Гость
Ответить в этой теме...

×   Вставлено с форматированием.   Вставить как обычный текст

  Разрешено использовать не более 75 эмодзи.

×   Ваша ссылка была автоматически встроена.   Отображать как обычную ссылку

×   Ваш предыдущий контент был восстановлен.   Очистить редактор

×   Вы не можете вставлять изображения напрямую. Загружайте или вставляйте изображения по ссылке.

×
×
  • Создать...