highlight.js library. Highlight.js highlights syntax in code examples on blogs, forums and in fact on any web pages. It´s very easy to use because it works automatically: finds blocks of code, detects a language, highlights it. * Version: 0.6.2 * Author: OWenT * Author URI: https://owent.net/ * License: 3-clause BSD */ $PLUGIN_DIR = plugins_url() . '/' . dirname(plugin_basename(__FILE__)); /** * Get version of this plugins */ function hljs_get_version() { return '0.6.2'; } /** * Get version of Highlight.js */ function hljs_get_lib_version() { return '9.12.0'; } /** * list cdn list */ function hljs_cdn_list() { return array( 'local' => array( 'cdn' => 'local', 'desc' => __('local', 'wp-code-highlight.js'), 'css' => '', 'js' => '', 'readme' => '', ), 'CdnJs' => array( 'cdn' => '//cdnjs.cloudflare.com/ajax/libs/highlight.js/' . hljs_get_lib_version(), 'desc' => __('Public CDN', 'wp-code-highlight.js') . ': cdnjs (' . __('highlightjs.org recommend', 'wp-code-highlight.js') . ')', 'css' => '.min', 'js' => '.min', 'readme' => 'https://cdnjs.com/libraries/highlight.js' ), 'BootCSS' => array( 'cdn' => '//cdn.bootcss.com/highlight.js/' . hljs_get_lib_version(), // 'cdn' => '//cdn.bootcss.com/highlight.js/9.2.0', 'desc' => __('Public CDN', 'wp-code-highlight.js') . ': BootCSS', 'css' => '.min', 'js' => '.min', 'readme' => 'http://www.bootcdn.cn/highlight.js/' ), 'jsDelivr' => array( //'cdn' => '//cdn.jsdelivr.net/highlight.js/' . hljs_get_lib_version(), 'cdn' => '//cdn.jsdelivr.net/highlight.js/9.11.0', 'desc' => __('Public CDN', 'wp-code-highlight.js') . ': jsDelivr (' . __('highlightjs.org recommend', 'wp-code-highlight.js') . ') ' . __('lastest version', 'wp-code-highlight.js') . ': 9.11.0)', 'css' => '.min', 'js' => '.min', 'readme' => 'http://www.jsdelivr.com/#!highlight.js' ), 'MaxCDN' => array( //'cdn' => '//oss.maxcdn.com/highlight.js/' . hljs_get_lib_version(), 'cdn' => '//oss.maxcdn.com/highlight.js/9.11.0', 'desc' => __('Public CDN', 'wp-code-highlight.js') . ': MaxCDN ' . __('lastest version', 'wp-code-highlight.js') . ': 9.11.0)', 'css' => '.min', 'js' => '.min', 'readme' => 'http://osscdn.com/#/highlight.js' ), 'Baidu' => array( //'cdn' => 'http://apps.bdimg.com/libs/highlight.js/' . hljs_get_lib_version(), //'cdn' => '//openapi.baidu.com/libs/highlight.js/' . hljs_get_lib_version(), 'cdn' => '//openapi.baidu.com/libs/highlight.js/9.1.0', 'desc' => __('Public CDN', 'wp-code-highlight.js') . ': ' . __('Baidu', 'wp-code-highlight.js') . __('lastest version', 'wp-code-highlight.js') . ': 9.1.0)', 'css' => '.min', 'js' => '.min', 'readme' => 'http://cdn.code.baidu.com/#highlight.js' ) ); } /** * Plugin Installation: * - create configuration keys */ function hljs_install() { add_option('hljs_code_option', array( 'location' => 'local', 'package' => 'common', 'theme' => 'default', 'hljs_option' => array( 'tabReplace' => ' ', 'classPrefix' => '', 'useBR' => false, 'languages' => '' ), 'additional_css' => "code.hljs { /*margin: 5px;*/ }", 'syntaxhighlighter_compatible' => false, 'crayonsyntaxhighlighter_compatible' => false, 'prettify_compatible' => false, 'shortcode' => false, 'custom_lang' => array() )); } register_activation_hook(__FILE__, 'hljs_install'); /** * Plugin Deinstallation * - delete configuration keys */ function hljs_deinstall() { delete_option('hljs_code_option'); } register_deactivation_hook(__FILE__, 'hljs_deinstall'); /** * Get option of this plugin */ function hljs_get_option($item) { $res = get_option('hljs_code_option'); if (empty($res) || !isset($res[$item])) return null; return $res[$item]; } /** * Set option of this plugin */ function hljs_set_option($item, $val) { $res = get_option('hljs_code_option'); if (empty($res)) $res = array(); $res[$item] = $val; update_option('hljs_code_option', $res); return $val; } /** * Get option of highlight.js */ function hljs_get_lib_option($item) { $res = hljs_get_option('hljs_option'); if (empty($res) || !isset($res[$item])) return ''; return $res[$item]; } function hljs_remove_ex_mode() { $upload_options = get_option('hljs_code_option'); if (!empty($upload_options)) { $opt_packs = $upload_options['package']; // ex mode already deleted, so convert to custom mode if ('ex' == $opt_packs) { $upload_options['package'] = 'custom'; $upload_options['custom_lang'] = array('actionscript', 'applescript', 'cmake', 'capnproto', 'd', 'dos', 'erlang', 'fsharp', 'go', 'less', 'lisp', 'lua', 'matlab', 'protobuf', 'profile', 'scala', 'tex', 'typescript'); update_option('hljs_code_option', $upload_options); } hljs_generate_custom_pack(); } } /** * Attach Highlight.js to the current page * - attach highlight.pack.js * - attach colorscheme stylesheet */ function hljs_include() { global $PLUGIN_DIR; $hljs_code_option = get_option('hljs_code_option'); $hljs_location = $hljs_code_option['location']; $hljs_package = hljs_get_option('package'); $hljs_cdn_list = hljs_cdn_list(); $hljs_cdn_info = $hljs_cdn_list['local']; if (!empty($hljs_cdn_list[$hljs_location])) $hljs_cdn_info = $hljs_cdn_list[$hljs_location]; // inject js & css file if ( 'local' == $hljs_cdn_info['cdn'] ) { if ('ex' == $hljs_package) { hljs_remove_ex_mode(); } $dep_libs = array('jquery'); if ('custom' == $hljs_package) { wp_enqueue_script( 'hljs_preload', $PLUGIN_DIR . '/highlight.common.pack.js', $dep_libs, hljs_get_version(), true ); $dep_libs = array('hljs_preload'); } wp_enqueue_script( 'hljs', $PLUGIN_DIR . '/highlight.' . $hljs_package .'.pack.js', $dep_libs, hljs_get_version(), true ); wp_enqueue_style( 'hljstheme', $PLUGIN_DIR . '/styles/' . $hljs_code_option['theme'] . '.css', array(), hljs_get_version() ); } else { wp_enqueue_script( 'hljs', $hljs_cdn_info['cdn'] . '/highlight' . $hljs_cdn_info['js'] . '.js', array('jquery'), hljs_get_version(), true ); wp_enqueue_style( 'hljstheme', $hljs_cdn_info['cdn'] . '/styles/' . $hljs_code_option['theme'] . $hljs_cdn_info['css'] . '.css', array(), hljs_get_version() ); // additional languages $custom_addition_langs = hljs_get_option('custom_lang'); if(!empty($custom_addition_langs)) { foreach(hljs_get_option('custom_lang') as $lang) { wp_enqueue_script( 'hljs_lang_' . $lang, $hljs_cdn_info['cdn'] . '/languages/' . $lang . $hljs_cdn_info['js'] . '.js', array('hljs'), hljs_get_version(), true ); } } } } add_action('wp_head', 'hljs_include'); /** * Attach init code to the current page */ function hljs_append_init_codes() { $hljs_code_option = get_option('hljs_code_option'); // inject init script foreach ($hljs_code_option['hljs_option'] as $key => $val) { if (!empty($val)) { $hljs_lib_config[$key] = $val; } } ?> $val) { ?> 'Common(about 40+KB)', 'all' => 'All(about 400+KB)', 'custom' => 'Custom', ); foreach($pkgs as $key => $val) { ?>' . __('Settings') . ''; } return $links; } add_filter('plugin_action_links', 'hljs_add_settings_link', 10, 2); /** * Add BB-Tag for highlighting. * * Usage: [CODE lang=cpp bbcode=enable]...[/CODE] */ function hljs_code_handler($attrs, $content) { $language = ''; if (!empty($attrs['lang'])) $language = "class=\"${attrs['lang']}\""; $enable_inner_bbcode = false; if(isset($attrs['bbcode'])) { $enable_inner_bbcode = true; if('disable' === $attrs['bbcode'] || 0 === $attrs['bbcode']) { $enable_inner_bbcode = false; } } if($enable_inner_bbcode) { return "
" . do_shortcode(ltrim($content, "\n")) . '
'; } else { return "
" . ltrim($content, "\n") . '
'; } } if (hljs_get_option('shortcode')) { add_shortcode('code', 'hljs_code_handler'); } function hljs_generate_custom_pack() { // generate custom language pack $ret = TRUE; $opt_loc = hljs_get_option('location'); $opt_packs = hljs_get_option('package'); $opt_langs = hljs_get_option('custom_lang'); $plugin_root_dir = plugin_dir_path( __FILE__ ); if ('local' == $opt_loc && 'custom' == $opt_packs) { $custom_pack_file = $plugin_root_dir . DIRECTORY_SEPARATOR . 'highlight.custom.pack.js'; if ( FALSE === file_put_contents($custom_pack_file, '') ) { echo '

' . __('Write custom highlight language package to', 'wp-code-highlight.js') . ' ' . $custom_pack_file . __('failed, please grant write permission to it.', 'wp-code-highlight.js') . '

'; $ret = FALSE; } else { foreach($opt_langs as $language_name) { $file_name = $language_name . '.min.js'; $full_path = $plugin_root_dir . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . $file_name; if (file_exists($full_path)) { $fc = file_get_contents($full_path); file_put_contents($custom_pack_file, $fc . PHP_EOL, FILE_APPEND); } else { echo '

' . __('Language file', 'wp-code-highlight.js') . ' ' . $file_name . ' ' . __('not found', 'wp-code-highlight.js') . ', ' . __('ignored', 'wp-code-highlight.js') . '

'; } } echo '

' . __('Generate custom highlight language package done.', 'wp-code-highlight.js') . '

'; } } else { // check if CDN available $all_available_cdns = hljs_cdn_list(); if(empty($all_available_cdns[$opt_loc])) { $default_cdn = 'CdnJs'; foreach($all_available_cdns as $key => $val) { if ('local' != $key) { $default_cdn = $key; break; } } hljs_set_option('location', $default_cdn); echo '

' . __('CDN ', 'wp-code-highlight.js') . $opt_loc . __(' not available now', 'wp-code-highlight.js') . '

'; echo '

' . __('Use default CDN ', 'wp-code-highlight.js') . $default_cdn . '

'; } } // rename style to default when filename is changed by highlight.js $style_name = hljs_get_option('theme'); if (false == file_exists($plugin_root_dir . DIRECTORY_SEPARATOR . 'styles' . DIRECTORY_SEPARATOR . $style_name . '.css')) { hljs_set_option('theme', 'default'); echo '

' . __('Style named ' . $style_name . ' is unavailable, maybe highlight.js has changed the name. Theme changed to default now', 'wp-code-highlight.js') . '

'; } return $ret; } function hljs_on_update_complete($plugin, $data) { if (!empty($data) && !empty($data['type']) && 'plugin' == $data['type'] && 'update' == $data['action']) { $this_file_name = basename(__FILE__); $rebuild_flag = false; foreach($data['plugins'] as $updated_file) { if ($this_file_name == basename($updated_file)) { $rebuild_flag = true; } } if ($rebuild_flag) { hljs_generate_custom_pack(); } } } add_action('upgrader_process_complete', 'hljs_on_update_complete', 10, 2); /** * Highlight.js Settings Page */ function hljs_settings_page() { global $PLUGIN_DIR; if (isset( $_POST['cmd'] ) && $_POST['cmd'] == 'hljs_save') { $upload_options = array( 'location' => $_POST['hljs_location'], 'package' => $_POST['hljs_package'], 'theme' => $_POST['hljs_theme'], 'hljs_option' => array( 'tabReplace' => $_POST['hljs_option_tab_replace'], 'classPrefix' => $_POST['hljs_option_class_prefix'], 'useBR' => (isset($_POST['hljs_option_use_br']) && $_POST['hljs_option_use_br'])? true: false, 'languages' => $_POST['hljs_option_languages'] ), 'additional_css' => $_POST['hljs_additional_css'], 'syntaxhighlighter_compatible' => (isset($_POST['hljs_syntaxhighlighter_compatible']) && $_POST['hljs_syntaxhighlighter_compatible'])? true: false, 'prettify_compatible' => (isset($_POST['hljs_prettify_compatible']) && $_POST['hljs_prettify_compatible'])? true: false, 'crayonsyntaxhighlighter_compatible' => (isset($_POST['hljs_crayonsyntaxhighlighter_compatible']) && $_POST['hljs_crayonsyntaxhighlighter_compatible'])? true: false, 'shortcode' => (isset($_POST['hljs_enable_shortcode']) && $_POST['hljs_enable_shortcode'])? true: false, 'custom_lang' => hljs_get_option('custom_lang') ); // generate custom language pack if ('local' == $upload_options['location'] && 'custom' == $upload_options['package']) { $upload_options['custom_lang'] = array(); $plugin_root_dir = plugin_dir_path( __FILE__ ); foreach($_POST as $key => $val) { $suffix = substr($key, -3); if (('.js' == $suffix || '_js' == $suffix )&& intval($val) == 1) { $language_name = substr($key, 0, strlen($key) - 3); $file_name = $language_name . '.min.js'; $full_path = $plugin_root_dir . DIRECTORY_SEPARATOR . 'languages' . DIRECTORY_SEPARATOR . $file_name; if (file_exists($full_path)) { array_push($upload_options['custom_lang'], $language_name); } else { echo '

' . __('Language file', 'wp-code-highlight.js') . ' ' . $file_name . ' ' . __('not found', 'wp-code-highlight.js') . ', ' . __('ignored', 'wp-code-highlight.js') . '

'; } } } } update_option('hljs_code_option', $upload_options); $res = hljs_generate_custom_pack(); if ($res === FALSE) { echo '

' . __('Save configurations failed.', 'wp-code-highlight.js') . '

'; } else { echo '

' . __('All configurations successfully saved.', 'wp-code-highlight.js') . '

'; } } ?>

Current Highlight.js Version:

Support List:

Common

Other


https://highlightjs.org/static/demo/', 'wp-code-highlight.js'); ?>
: https://highlightjs.org/ for detail', 'wp-code-highlight.js'); ?>





/>



/>
/>
/>

/>