Помощь - Поиск - Пользователи - Календарь
Полная версия этой страницы: Раскраска кода
Форум разработчиков электроники ELECTRONIX.ru > Сайт и форум > Новости и обсуждения сайта и форума
andyp
Нельзя ли прикрутить раскраску кода для matlab/c++/c ?
megajohn
до сих пор *.cpp *.c нельзя прикреплять, так что до раскраски еще огого
andyp
Локально прикрутил раскраску с помощью 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);

})();


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



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



как переключать скины написано тут:
https://github.com/google/code-prettify/blo...ting_started.md

Если есть спецы по js, то буду рад любым замечаниям по улучшению.
СНБ
Цитата(andyp @ Mar 1 2016, 12:52) *
Нельзя ли прикрутить раскраску кода для matlab/c++/c ?

В смысле "раскраску"?
Синтаксическую подсветку что ли?
andyp
Цитата(СНБ @ May 23 2016, 18:25) *
В смысле "раскраску"?
Синтаксическую подсветку что ли?


Ну да.
Для просмотра полной версии этой страницы, пожалуйста, пройдите по ссылке.
Invision Power Board © 2001-2025 Invision Power Services, Inc.