WordPress套件《WP-GeSHi-Highlight》讓程式碼高亮度語法顯示

許多人在 WordPress 分享程式碼,在分享程式碼時,希望程式碼像在程式編輯器一樣,可以高亮度語法顯示。而程式碼高亮度語法顯示的工具很多,今天來介紹「WP-GeSHi-Highlight」這個工具,這個套件是師承 WP-Syntax,而且支援超多語法的,如果要分享程式碼,這個工具可以試看看。

 

WP-GeSHi-Highlight

 

◎◎Wordpress 套件小檔案◎◎
套件名稱:WP-GeSHi-Highlight
套件版本:1.3.1
語言介面:英文
套件性質:免費
官方網站:https://www.inote.tools/wp-geshi-highlight/

 

WP-GeSHi-Highlight 的用法如下,其中 lang=php 代表語法是 php,可以換成其它語言,詳見官方所列出的「支援語言列表」。而 line=”1″ 代表要顯示行號,行號從 1 開始。

  1. <pre lang="php" line="1">
  2. </pre>

而底下是展示範例,是以 Java 的程式碼來做展!

  1. /*
  2.  * Copyright 1994-2007 Sun Microsystems, Inc.  All Rights Reserved.
  3.  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4.  *
  5.  * This code is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License version 2 only, as
  7.  * published by the Free Software Foundation.  Sun designates this
  8.  * particular file as subject to the "Classpath" exception as provided
  9.  * by Sun in the LICENSE file that accompanied this code.
  10.  *
  11.  * This code is distributed in the hope that it will be useful, but WITHOUT
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.  * version 2 for more details (a copy is included in the LICENSE file that
  15.  * accompanied this code).
  16.  *
  17.  * You should have received a copy of the GNU General Public License version
  18.  * 2 along with this work; if not, write to the Free Software Foundation,
  19.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22.  * CA 95054 USA or visit www.sun.com if you need additional information or
  23.  * have any questions.
  24.  */
  25. package java.lang;
  26.  
  27. import java.io.*;
  28. import java.util.Properties;
  29. import java.util.PropertyPermission;
  30. import java.util.StringTokenizer;
  31. import java.security.AccessController;
  32. import java.security.PrivilegedAction;
  33. import java.security.AllPermission;
  34. import java.nio.channels.Channel;
  35. import java.nio.channels.spi.SelectorProvider;
  36. import sun.nio.ch.Interruptible;
  37. import sun.net.InetAddressCachePolicy;
  38. import sun.reflect.Reflection;
  39. import sun.security.util.SecurityConstants;
  40. import sun.reflect.annotation.AnnotationType;
  41.  
  42. /**
  43.  * The &lt;code&gt;System&lt;/code&gt; class contains several useful class fields
  44.  * and methods. It cannot be instantiated.
  45.  *
  46.  * &lt;p&gt;Among the facilities provided by the &lt;code&gt;System&lt;/code&gt; class
  47.  * are standard input, standard output, and error output streams;
  48.  * access to externally defined properties and environment
  49.  * variables; a means of loading files and libraries; and a utility
  50.  * method for quickly copying a portion of an array.
  51.  *
  52.  * @author  unascribed
  53.  * @since   JDK1.0
  54.  */
  55. public final class System {
  56.  
  57.     /* First thing---register the natives */
  58.     private static native void registerNatives();
  59.     static {
  60.         registerNatives();
  61.     }
  62.  
  63.     /** Don't let anyone instantiate this class */
  64.     private System() {
  65.     }
  66.  
  67.     /**
  68.      * The "standard" input stream. This stream is already
  69.      * open and ready to supply input data. Typically this stream
  70.      * corresponds to keyboard input or another input source specified by
  71.      * the host environment or user.
  72.      */
  73.     public final static InputStream in = nullInputStream();
  74.  
  75.     /**
  76.      * The "standard" output stream. This stream is already
  77.      * open and ready to accept output data. Typically this stream
  78.      * corresponds to display output or another output destination
  79.      * specified by the host environment or user.
  80.      * &lt;p&gt;
  81.      * For simple stand-alone Java applications, a typical way to write
  82.      * a line of output data is:
  83.      * &lt;blockquote&gt;&lt;pre&gt;
  84.      *     System.out.println(data)
  85.      * &lt;/pre&gt;&lt;/blockquote&gt;
  86.      * &lt;p&gt;
  87.      * See the &lt;code&gt;println&lt;/code&gt; methods in class &lt;code&gt;PrintStream&lt;/code&gt;.
  88.      *
  89.      * @see     java.io.PrintStream#println()
  90.      * @see     java.io.PrintStream#println(boolean)
  91.      * @see     java.io.PrintStream#println(char)
  92.      * @see     java.io.PrintStream#println(char[])
  93.      * @see     java.io.PrintStream#println(double)
  94.      * @see     java.io.PrintStream#println(float)
  95.      * @see     java.io.PrintStream#println(int)
  96.      * @see     java.io.PrintStream#println(long)
  97.      * @see     java.io.PrintStream#println(java.lang.Object)
  98.      * @see     java.io.PrintStream#println(java.lang.String)
  99.      */
  100.     public final static PrintStream out = nullPrintStream();
  101.  
  102.     /**
  103.      * The "standard" error output stream. This stream is already
  104.      * open and ready to accept output data.
  105.      * &lt;p&gt;
  106.      * Typically this stream corresponds to display output or another
  107.      * output destination specified by the host environment or user. By
  108.      * convention, this output stream is used to display error messages
  109.      * or other information that should come to the immediate attention
  110.      * of a user even if the principal output stream, the value of the
  111.      * variable &lt;code&gt;out&lt;/code&gt;, has been redirected to a file or other
  112.      * destination that is typically not continuously monitored.
  113.      */
  114.     public final static PrintStream err = nullPrintStream();
  115.  
  116.     /* The security manager for the system.
  117.      */
  118.     private static volatile SecurityManager security = null;
  119.  
  120.     /**
  121.      * Reassigns the "standard" input stream.
  122.      *
  123.      * &lt;p&gt;First, if there is a security manager, its &lt;code&gt;checkPermission&lt;/code&gt;
  124.      * method is called with a &lt;code&gt;RuntimePermission("setIO")&lt;/code&gt; permission
  125.      *  to see if it's ok to reassign the "standard" input stream.
  126.      * &lt;p&gt;
  127.      *
  128.      * @param in the new standard input stream.
  129.      *
  130.      * @throws SecurityException
  131.      *        if a security manager exists and its
  132.      *        &lt;code&gt;checkPermission&lt;/code&gt; method doesn't allow
  133.      *        reassigning of the standard input stream.
  134.      *
  135.      * @see SecurityManager#checkPermission
  136.      * @see java.lang.RuntimePermission
  137.      *
  138.      * @since   JDK1.1
  139.      */
  140.     public static void setIn(InputStream in) {
  141.         checkIO();
  142.         setIn0(in);
  143.     }
  144.  
  145.  
  146.     /**
  147.      * Returns the channel inherited from the entity that created this
  148.      * Java virtual machine.
  149.      *
  150.      * &lt;p&gt; This method returns the channel obtained by invoking the
  151.      * {@link java.nio.channels.spi.SelectorProvider#inheritedChannel
  152.      * inheritedChannel} method of the system-wide default
  153.      * {@link java.nio.channels.spi.SelectorProvider} object. &lt;/p&gt;
  154.      *
  155.      * &lt;p&gt; In addition to the network-oriented channels described in
  156.      * {@link java.nio.channels.spi.SelectorProvider#inheritedChannel
  157.      * inheritedChannel}, this method may return other kinds of
  158.      * channels in the future.
  159.      *
  160.      * @return  The inherited channel, if any, otherwise &lt;tt&gt;null&lt;/tt&gt;.
  161.      *
  162.      * @throws  IOException
  163.      *          If an I/O error occurs
  164.      *
  165.      * @throws  SecurityException
  166.      *          If a security manager is present and it does not
  167.      *          permit access to the channel.
  168.      *
  169.      * @since 1.5
  170.      */
  171.     public static Channel inheritedChannel() throws IOException {
  172.         return SelectorProvider.provider().inheritedChannel();
  173.     }
  174.  
  175.     private static void checkIO() {
  176.         SecurityManager sm = getSecurityManager();
  177.         if (sm != null) {
  178.             sm.checkPermission(new RuntimePermission("setIO"));
  179.         }
  180.     }
  181.  
  182.     private static native void setIn0(InputStream in);
  183.     private static native void setOut0(PrintStream out);
  184.     private static native void setErr0(PrintStream err);
  185.  
  186.     /**
  187.      * Sets the System security.
  188.      *
  189.      * &lt;p&gt; If there is a security manager already installed, this method first
  190.      * calls the security manager's &lt;code&gt;checkPermission&lt;/code&gt; method
  191.      * with a &lt;code&gt;RuntimePermission("setSecurityManager")&lt;/code&gt;
  192.      * permission to ensure it's ok to replace the existing
  193.      * security manager.
  194.      * This may result in throwing a &lt;code&gt;SecurityException&lt;/code&gt;.
  195.      *
  196.      * &lt;p&gt; Otherwise, the argument is established as the current
  197.      * security manager. If the argument is &lt;code&gt;null&lt;/code&gt; and no
  198.      * security manager has been established, then no action is taken and
  199.      * the method simply returns.
  200.      *
  201.      * @param      s   the security manager.
  202.      * @exception  SecurityException  if the security manager has already
  203.      *             been set and its &lt;code&gt;checkPermission&lt;/code&gt; method
  204.      *             doesn't allow it to be replaced.
  205.      * @see #getSecurityManager
  206.      * @see SecurityManager#checkPermission
  207.      * @see java.lang.RuntimePermission
  208.      */
  209.     public static
  210.     void setSecurityManager(final SecurityManager s) {
  211.         try {
  212.             s.checkPackageAccess("java.lang");
  213.         } catch (Exception e) {
  214.             // no-op
  215.         }
  216.         setSecurityManager0(s);
  217.     }
  218.  
  219.     private static synchronized
  220.     void setSecurityManager0(final SecurityManager s) {
  221.         SecurityManager sm = getSecurityManager();
  222.         if (sm != null) {
  223.             // ask the currently installed security manager if we
  224.             // can replace it.
  225.             sm.checkPermission(new RuntimePermission
  226.                                      ("setSecurityManager"));
  227.         }
  228.  
  229.         if ((s != null) &amp;&amp; (s.getClass().getClassLoader() != null)) {
  230.             // New security manager class is not on bootstrap classpath.
  231.             // Cause policy to get initialized before we install the new
  232.             // security manager, in order to prevent infinite loops when
  233.             // trying to initialize the policy (which usually involves
  234.             // accessing some security and/or system properties, which in turn
  235.             // calls the installed security manager's checkPermission method
  236.             // which will loop infinitely if there is a non-system class
  237.             // (in this case: the new security manager class) on the stack).
  238.             AccessController.doPrivileged(new PrivilegedAction&lt;Object&gt;() {
  239.                 public Object run() {
  240.                     s.getClass().getProtectionDomain().implies
  241.                         (SecurityConstants.ALL_PERMISSION);
  242.                     return null;
  243.                 }
  244.             });
  245.         }
  246.  
  247.         security = s;
  248.         InetAddressCachePolicy.setIfNotSet(InetAddressCachePolicy.FOREVER);
  249.     }
  250.  
  251.  
  252.     /**
  253.      * Removes the system property indicated by the specified key.
  254.      * &lt;p&gt;
  255.      * First, if a security manager exists, its
  256.      * &lt;code&gt;SecurityManager.checkPermission&lt;/code&gt; method
  257.      * is called with a &lt;code&gt;PropertyPermission(key, "write")&lt;/code&gt;
  258.      * permission. This may result in a SecurityException being thrown.
  259.      * If no exception is thrown, the specified property is removed.
  260.      * &lt;p&gt;
  261.      *
  262.      * @param      key   the name of the system property to be removed.
  263.      * @return     the previous string value of the system property,
  264.      *             or &lt;code&gt;null&lt;/code&gt; if there was no property with that key.
  265.      *
  266.      * @exception  SecurityException  if a security manager exists and its
  267.      *             &lt;code&gt;checkPropertyAccess&lt;/code&gt; method doesn't allow
  268.      *              access to the specified system property.
  269.      * @exception  NullPointerException if &lt;code&gt;key&lt;/code&gt; is
  270.      *             &lt;code&gt;null&lt;/code&gt;.
  271.      * @exception  IllegalArgumentException if &lt;code&gt;key&lt;/code&gt; is empty.
  272.      * @see        #getProperty
  273.      * @see        #setProperty
  274.      * @see        java.util.Properties
  275.      * @see        java.lang.SecurityException
  276.      * @see        java.lang.SecurityManager#checkPropertiesAccess()
  277.      * @since 1.5
  278.      */
  279.     public static String clearProperty(String key) {
  280.         checkKey(key);
  281.         SecurityManager sm = getSecurityManager();
  282.         if (sm != null) {
  283.             sm.checkPermission(new PropertyPermission(key, "write"));
  284.         }
  285.  
  286.         return (String) props.remove(key);
  287.     }
  288.  
  289.     private static void checkKey(String key) {
  290.         if (key == null) {
  291.             throw new NullPointerException("key can't be null");
  292.         }
  293.         if (key.equals("")) {
  294.             throw new IllegalArgumentException("key can't be empty");
  295.         }
  296.     }
  297.  
  298.     /**
  299.      * Enable or disable finalization on exit; doing so specifies that the
  300.      * finalizers of all objects that have finalizers that have not yet been
  301.      * automatically invoked are to be run before the Java runtime exits.
  302.      * By default, finalization on exit is disabled.
  303.      *
  304.      * &lt;p&gt;If there is a security manager,
  305.      * its &lt;code&gt;checkExit&lt;/code&gt; method is first called
  306.      * with 0 as its argument to ensure the exit is allowed.
  307.      * This could result in a SecurityException.
  308.      *
  309.      * @deprecated  This method is inherently unsafe.  It may result in
  310.      *      finalizers being called on live objects while other threads are
  311.      *      concurrently manipulating those objects, resulting in erratic
  312.      *      behavior or deadlock.
  313.      * @param value indicating enabling or disabling of finalization
  314.      * @throws  SecurityException
  315.      *        if a security manager exists and its &lt;code&gt;checkExit&lt;/code&gt;
  316.      *        method doesn't allow the exit.
  317.      *
  318.      * @see     java.lang.Runtime#exit(int)
  319.      * @see     java.lang.Runtime#gc()
  320.      * @see     java.lang.SecurityManager#checkExit(int)
  321.      * @since   JDK1.1
  322.      */
  323.     @Deprecated
  324.     public static void runFinalizersOnExit(boolean value) {
  325.         Runtime.getRuntime().runFinalizersOnExit(value);
  326.     }
  327.  
  328.     /**
  329.      * Maps a library name into a platform-specific string representing
  330.      * a native library.
  331.      *
  332.      * @param      libname the name of the library.
  333.      * @return     a platform-dependent native library name.
  334.      * @exception  NullPointerException if &lt;code&gt;libname&lt;/code&gt; is
  335.      *             &lt;code&gt;null&lt;/code&gt;
  336.      * @see        java.lang.System#loadLibrary(java.lang.String)
  337.      * @see        java.lang.ClassLoader#findLibrary(java.lang.String)
  338.      * @since      1.2
  339.      */
  340.     public static native String mapLibraryName(String libname);
  341.  
  342.     /**
  343.      * The following two methods exist because in, out, and err must be
  344.      * initialized to null.  The compiler, however, cannot be permitted to
  345.      * inline access to them, since they are later set to more sensible values
  346.      * by initializeSystemClass().
  347.      */
  348.     private static InputStream nullInputStream() throws NullPointerException {
  349.         if (currentTimeMillis() &gt; 0) {
  350.             return null;
  351.         }
  352.         throw new NullPointerException();
  353.     }
  354.  
  355.     private static PrintStream nullPrintStream() throws NullPointerException {
  356.         if (currentTimeMillis() &gt; 0) {
  357.             return null;
  358.         }
  359.         throw new NullPointerException();
  360.     }
  361.  
  362.     /**
  363.      * Initialize the system class.  Called after thread initialization.
  364.      */
  365.     private static void initializeSystemClass() {
  366.         props = new Properties();
  367.         initProperties(props);
  368.         sun.misc.Version.init();
  369.         FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
  370.         FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
  371.         FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
  372.         setIn0(new BufferedInputStream(fdIn));
  373.         setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
  374.         setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));
  375.  
  376.         // Load the zip library now in order to keep java.util.zip.ZipFile
  377.         // from trying to use itself to load this library later.
  378.         loadLibrary("zip");
  379.  
  380.         // Setup Java signal handlers for HUP, TERM, and INT (where available).
  381.         Terminator.setup();
  382.  
  383.         // The order in with the hooks are added here is important as it
  384.         // determines the order in which they are run.
  385.         // (1)Console restore hook needs to be called first.
  386.         // (2)Application hooks must be run before calling deleteOnExitHook.
  387.         Shutdown.add(sun.misc.SharedSecrets.getJavaIOAccess().consoleRestoreHook());
  388.         Shutdown.add(ApplicationShutdownHooks.hook());
  389.         Shutdown.add(sun.misc.SharedSecrets.getJavaIODeleteOnExitAccess());
  390.  
  391.         // Initialize any miscellenous operating system settings that need to be
  392.         // set for the class libraries. Currently this is no-op everywhere except
  393.         // for Windows where the process-wide error mode is set before the java.io
  394.         // classes are used.
  395.         sun.misc.VM.initializeOSEnvironment();
  396.  
  397.         // Set the maximum amount of direct memory.  This value is controlled
  398.         // by the vm option -XX:MaxDirectMemorySize=&lt;size&gt;.  This method acts
  399.         // as an initializer only if it is called before sun.misc.VM.booted().
  400.         sun.misc.VM.maxDirectMemory();
  401.  
  402.         // Set a boolean to determine whether ClassLoader.loadClass accepts
  403.         // array syntax.  This value is controlled by the system property
  404.         // "sun.lang.ClassLoader.allowArraySyntax".  This method acts as
  405.         // an initializer only if it is called before sun.misc.VM.booted().
  406.         sun.misc.VM.allowArraySyntax();
  407.  
  408.         // Subsystems that are invoked during initialization can invoke
  409.         // sun.misc.VM.isBooted() in order to avoid doing things that should
  410.         // wait until the application class loader has been set up.
  411.         sun.misc.VM.booted();
  412.  
  413.         // The main thread is not added to its thread group in the same
  414.         // way as other threads; we must do it ourselves here.
  415.         Thread current = Thread.currentThread();
  416.         current.getThreadGroup().add(current);
  417.  
  418.         // Allow privileged classes outside of java.lang
  419.         sun.misc.SharedSecrets.setJavaLangAccess(new sun.misc.JavaLangAccess(){
  420.             public sun.reflect.ConstantPool getConstantPool(Class klass) {
  421.                 return klass.getConstantPool();
  422.             }
  423.             public void setAnnotationType(Class klass, AnnotationType type) {
  424.                 klass.setAnnotationType(type);
  425.             }
  426.             public AnnotationType getAnnotationType(Class klass) {
  427.                 return klass.getAnnotationType();
  428.             }
  429.             public &lt;E extends Enum&lt;E&gt;&gt;
  430.                     E[] getEnumConstantsShared(Class&lt;E&gt; klass) {
  431.                 return klass.getEnumConstantsShared();
  432.             }
  433.             public void blockedOn(Thread t, Interruptible b) {
  434.                 t.blockedOn(b);
  435.             }
  436.         });
  437.     }
  438.  
  439.     /* returns the class of the caller. */
  440.     static Class getCallerClass() {
  441.         // NOTE use of more generic Reflection.getCallerClass()
  442.         return Reflection.getCallerClass(3);
  443.     }
  444. }