Advertisement
Guest User

Untitled

a guest
Sep 29th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*******************************************************************************
  3. * Code Highlighting © 2010-2012, Bugo                                          *
  4. ********************************************************************************
  5. * Subs-Highlight.php                                                           *
  6. ********************************************************************************
  7. * License http://creativecommons.org/licenses/by-nc-nd/3.0/deed.ru CC BY-NC-ND *
  8. * Support and updates for this software can be found at http://dragomano.ru    *
  9. *******************************************************************************/
  10.  
  11. if (!defined('SMF'))
  12.     die('Hacking attempt...');
  13.  
  14.  
  15. global $darkThemeIDS, $darkThemeHighlightCSS, $lightThemeHighlightCSS;
  16. $darkThemeIDS = array(3, 5, 7, 9, 11, 13);
  17. $darkThemeHighlightCSS = "monokai_sublime.css";
  18. $lightThemeHighlightCSS = "vs.css";
  19.  
  20.  
  21. // Code tag
  22. function ch_code_tag(&$codes)
  23. {
  24.     global $modSettings, $txt, $settings;
  25.  
  26.     $codeWrapHTML1 = '<div class=\"block_code\" id=\"code_a" . $codecounter . "\"><pre><code>$1</code></pre></div>"';
  27.     $codeWrapHTML2 = '<div class=\"block_code\" id=\"code_b" . $codecounter . "\"><pre><code class=\"" . $data[1] . "\">$1</code></pre></div>"';
  28.  
  29.     if (!empty($modSettings['ch_enable']))
  30.     {
  31.         foreach ($codes as $tag => $dump)
  32.             if ($dump['tag'] == 'code') unset($codes[$tag]);
  33.            
  34.         $codes[] = array(
  35.             'tag' => 'code',
  36.             'type' => 'unparsed_content',
  37.             'validate' => isset($disabled['code']) ? null : create_function('&$tag, &$data, $disabled', '
  38.                global $txt;
  39.                // select all and copy code to clipboard mod
  40.                static $codecounter = 0;
  41.                $codecounter++;
  42.                $tag[\'content\'] = "<div class=\"codeheader\">" . $txt[\'smf238\'] . ": <a href=\"javascript:void(0)\" title=\"" . $txt[\'codeselectall\'] . "\" onclick=\"javascript:selectall(\'code_a" . $codecounter . "\');return false;\">" . $txt[\'codeselectall\'] . "</a></div>' . $codeWrapHTML1 . ';
  43.            '),
  44.             'block_level' => true,
  45.         );
  46.         $codes[] = array(
  47.             'tag' => 'code',
  48.             'type' => 'unparsed_equals_content',
  49.             'validate' => isset($disabled['code']) ? null : create_function('&$tag, &$data, $disabled', '
  50.                global $txt;
  51.                // select all and copy code to clipboard mod
  52.                static $codecounter = 0;
  53.                $codecounter++;
  54.                $tag[\'content\'] = "<div class=\"codeheader\">" . $txt[\'smf238\'] . ": " . $data[1] . " <a href=\"javascript:void(0)\" title=\"" . $txt[\'codeselectall\'] . "\" onclick=\"javascript:selectall(\'code_b" . $codecounter . "\');return false;\">" . $txt[\'codeselectall\'] . "</a></div>' . $codeWrapHTML2 . ';
  55.            '),
  56.             'block_level' => true,
  57.         );
  58.     }
  59.    
  60.     // Copyright Info
  61.     if (isset($context['current_action']) && $context['current_action'] == 'credits')
  62.         $context['copyrights']['mods'][] = '<a href="http://dragomano.ru/page/code-highlighting" target="_blank">Code Highlighting</a> &copy; 2010&ndash;2012, Bugo';
  63. }
  64.  
  65. // Code Highlighting
  66. function ch_load_theme()
  67. {
  68.     global $txt, $modSettings, $context, $settings, $darkThemeIDS, $darkThemeHighlightCSS, $lightThemeHighlightCSS;
  69.    
  70.     loadLanguage('Highlight');
  71.    
  72.     if (!isset($modSettings['ch_enable'])) updateSettings(array('ch_enable' => '1'));
  73.     if (!isset($modSettings['ch_tab'])) updateSettings(array('ch_tab' => '4'));
  74.    
  75.     // Highlight
  76.     if (!empty($modSettings['ch_enable']))
  77.     {
  78.         $i = 0;
  79.         $tab = '';
  80.         if (!empty($modSettings['ch_tab'])) {
  81.             while ($i < $modSettings['ch_tab']) {
  82.                 $tab .= ' ';
  83.                 $i++;
  84.             }
  85.         }
  86.  
  87.         $style_css = 'default.css';
  88.         if (in_array($settings['theme_id'], $darkThemeIDS))
  89.         {
  90.             $style_css = $darkThemeHighlightCSS;
  91.         }
  92.         else
  93.         {
  94.             $style_css = $lightThemeHighlightCSS;
  95.         }
  96.    
  97.         $context['html_headers'] .= '
  98.    <link rel="stylesheet" type="text/css" href="' . $settings['default_theme_url'] . '/styles/' . $style_css . '" />';
  99.    
  100.         if (isset($_REQUEST['sa']) && $_REQUEST['sa'] == 'showoperations') return;
  101.    
  102.         if (!WIRELESS && !in_array($context['current_action'], array('helpadmin', 'printpage')))
  103.             $context['html_headers'] .= '
  104.        <script type="text/javascript" src="' . $settings['default_theme_url'] . '/highlight.pack.js"></script>
  105.        <script type="text/javascript">
  106.        hljs.configure({
  107.          tabReplace: "' . $tab . '",
  108.          useBR: true
  109.        });
  110.        hljs.initHighlightingOnLoad();
  111.        </script>';
  112.                
  113.     }
  114.        
  115.     // Preview
  116.     if (!empty($modSettings['ch_enable']) && in_array($context['current_action'], array('post', 'post2')))
  117.         $context['html_headers'] .= '
  118.        <script type="text/javascript">
  119.            var highlightPreviewCodeBlocks = function(){
  120.                if (highlightPreviewCodeBlocks.calling)
  121.                    return;
  122.                highlightPreviewCodeBlocks.calling = true;
  123.                var blocks = document.querySelectorAll("#preview_body pre code");
  124.                Array.prototype.forEach.call(blocks, hljs.highlightBlock);
  125.                highlightPreviewCodeBlocks.calling = false;
  126.            };
  127.            
  128.            document.querySelector("#preview_body").addEventListener("DOMSubtreeModified", highlightPreviewCodeBlocks, false);
  129.        </script>';
  130.  
  131. }
  132.  
  133. function ch_buffer(&$buffer)
  134. {
  135.     global $modSettings, $txt, $settings, $darkThemeIDS, $darkThemeHighlightCSS, $lightThemeHighlightCSS;
  136.  
  137.     $i = 0;
  138.     $tab = '';
  139.     if (!empty($modSettings['ch_tab'])) {
  140.         while ($i < $modSettings['ch_tab']) {
  141.             $tab .= ' ';
  142.             $i++;
  143.         }
  144.     }
  145.  
  146.     $style_css = 'default.css';
  147.     if (in_array($settings['theme_id'], $darkThemeIDS))
  148.     {
  149.         $style_css = $darkThemeHighlightCSS;
  150.     }
  151.     else
  152.     {
  153.         $style_css = $lightThemeHighlightCSS;
  154.     }
  155.    
  156.     $search = $replace = '';
  157.    
  158.     if (!empty($modSettings['ch_enable']) && isset($txt['operation_title']))
  159.     {
  160.         $css = "\n\t\t" . '<link rel="stylesheet" type="text/css" href="' . $settings['default_theme_url'] . '/styles/' . $style_css . '" />';
  161.         $js = "\n\t\t" . '<script type="text/javascript" src="' . $settings['default_theme_url'] . '/highlight.pack.js"></script>
  162.        <script type="text/javascript">
  163.        hljs.configure({
  164.          tabReplace: "' . $tab . '",
  165.          useBR: true
  166.        });
  167.        hljs.initHighlightingOnLoad();
  168.        </script>';
  169.         $search = '<title>' . $txt['operation_title'] . '</title>';
  170.         $replace = $search . $css . $js;
  171.     }
  172.    
  173.     return (isset($_REQUEST['xml']) ? $buffer : str_replace($search, $replace, $buffer));
  174. }
  175.  
  176. function ch_admin_areas(&$admin_areas)
  177. {
  178.     global $txt;
  179.    
  180.     loadLanguage('Highlight');
  181.    
  182.     $context['admin_areas']['config']['areas']['modsettings']['subsections']['highlight'] = array($txt['ch_title']);
  183. }
  184.  
  185. function ch_modifications(&$subActions)
  186. {
  187.     $subActions['highlight'] = 'ch_settings';
  188. }
  189.  
  190. // Modification settings
  191. function ch_settings()
  192. {
  193.     global $txt, $context, $scripturl, $modSettings;
  194.    
  195.     loadLanguage('Highlight');
  196.    
  197.     $context['settings_title'] = $txt['ch_settings'];
  198.     $context['post_url'] = $scripturl . '?action=featuresettings2;save;sa=highlight';
  199.    
  200.     $config_vars = array(
  201.         array('check', 'ch_enable'),
  202.         array('int', 'ch_tab')
  203.     );  
  204.    
  205.     if (!empty($modSettings['ch_enable']) && function_exists('file_get_contents')) $config_vars[] = array('callback', 'ch_example');
  206.    
  207.     // Saving?
  208.     if (isset($_GET['save']))
  209.     {
  210.         checkSession();
  211.         saveDBSettings($config_vars);
  212.         redirectexit('action=featuresettings;;sa=highlight');
  213.     }
  214.    
  215.     prepareDBSettingContext($config_vars);
  216. }
  217.  
  218. // Example template
  219. function template_callback_ch_example()
  220. {
  221.     global $txt, $context, $settings;
  222.    
  223.     loadLanguage('Highlight');
  224.    
  225.     if (file_exists($settings['default_theme_dir'] . '/css/admin.css'))
  226.     {
  227.         $file = @file_get_contents($settings['default_theme_dir'] . '/css/admin.css');
  228.         $file = parse_bbc('[code]' . $file . '[/code]');
  229.         echo '<dl><strong>' . $txt['ch_example'] . '</strong>' . $file . '</dl><dt></dt><dd></dd>';
  230.     }
  231. }
  232.  
  233. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement