Yusi主题高亮代码生成工具

Yusi主题高亮代码生成工具

在使用Yusi的主题时,发现其带有一款集成到主题中的代码高亮显示功能。只需要在使用如下的设置方式即可实现代码高亮显示。

<pre class="prettyprint linenums" >代码内容</pre>

但在Windows Live Writer文本编辑工具中的源代码视图中编辑HTML代码过于繁琐,因此,这里使用Java实现,代码高亮的自动生成工具。

使用说明

将需要显示的代码段复制到工具指定的目录的文件中,运行该程序后,控制台将打印出生成的HTML高亮代码,直接复制即可使用。

使用测试

单行测试

In [17]: count_by_occupation = user_fields.map(lambda fields:(fields[3], 1)).reduceByKey(lambda x, y: x + y).collect() 

多行代码测试

[yitian@localhost Desktop]$ IPYTHON=1 IPYTHON_OPTS=”–pylab” /usr/spark/spark-1.6.2-bin-hadoop2.6/bin/pyspark
Python 2.7.13 |Anaconda, Inc.| (default, Sep 30 2017, 13:07:41) 
Type “copyright”, “credits” or “license” for more information.

IPython 5.4.1 — An enhanced Interactive Python.
?         -> Introduction and overview of IPython’s features.
%quickref -> Quick reference.
help      -> Python’s own help system.
object?   -> Details about ‘object’, use ‘object??’ for extra details.
Using matplotlib backend: Qt5Agg
Using Spark’s default log4j profile: org/apache/spark/log4j-defaults.properties
17/10/03 09:01:03 INFO SparkContext: Running Spark version 1.6.2
17/10/03 09:01:03 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform… using builtin-java classes where applicable
17/10/03 09:01:03 WARN Utils: Your hostname, localhost.localdomain resolves to a loopback address: 127.0.0.1, but we couldn’t find any external IP address!
17/10/03 09:01:03 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address
17/10/03 09:01:03 INFO SecurityManager: Changing view acls to: yitian
17/10/03 09:01:03 INFO SecurityManager: Changing modify acls to: yitian
17/10/03 09:01:03 INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: Set(yitian); users with modify permissions: Set(yitian)
17/10/03 09:01:04 INFO Utils: Successfully started service ‘sparkDriver’ on port 37228.
17/10/03 09:01:05 INFO Slf4jLogger: Slf4jLogger started
17/10/03 09:01:05 INFO Remoting: Starting remoting
17/10/03 09:01:05 INFO Remoting: Remoting started; listening on addresses :[akka.tcp://sparkDriverActorSystem@127.0.0.1:42099]
17/10/03 09:01:05 INFO Utils: Successfully started service ‘sparkDriverActorSystem’ on port 42099.
17/10/03 09:01:05 INFO SparkEnv: Registering MapOutputTracker
17/10/03 09:01:05 INFO SparkEnv: Registering BlockManagerMaster
17/10/03 09:01:05 INFO DiskBlockManager: Created local directory at /tmp/blockmgr-7468b651-acaf-493a-94a2-4c8f4e49c680
17/10/03 09:01:05 INFO MemoryStore: MemoryStore started with capacity 517.4 MB
17/10/03 09:01:05 INFO SparkEnv: Registering OutputCommitCoordinator
17/10/03 09:01:06 INFO Utils: Successfully started service ‘SparkUI’ on port 4040.
17/10/03 09:01:06 INFO SparkUI: Started SparkUI at http://127.0.0.1:4040
17/10/03 09:01:06 INFO Executor: Starting executor ID driver on host localhost
17/10/03 09:01:06 INFO Utils: Successfully started service ‘org.apache.spark.network.netty.NettyBlockTransferService’ on port 42577.
17/10/03 09:01:06 INFO NettyBlockTransferService: Server created on 42577
17/10/03 09:01:06 INFO BlockManagerMaster: Trying to register BlockManager
17/10/03 09:01:06 INFO BlockManagerMasterEndpoint: Registering block manager localhost:42577 with 517.4 MB RAM, BlockManagerId(driver, localhost, 42577)
17/10/03 09:01:06 INFO BlockManagerMaster: Registered BlockManager
Welcome to
      ____              __
     / __/__  ___ _____/ /__
    _\ \/ _ \/ _ `/ __/  ‘_/
   /__ / .__/\_,_/_/ /_/\_\   version 1.6.2
      /_/

Using Python version 2.7.13 (default, Sep 30 2017 13:07:41)
SparkContext available as sc, HiveContext available as sqlContext.

In [1]:

工具源代码

package cn.yitian;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class WPCoding {
	
	/*
	 * 从txt文件中读取内容
	 */
    public static String txt2String(File file){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法,一次读一行
                result.append(System.lineSeparator()+s);
            }
            br.close();    
        }catch(Exception e){
            e.printStackTrace();
        }
        return result.toString();
    }
	
	public static void main(String[] args) {
		
		File file = new File("P:/Workspaces/MyEclipse 10/WPCoding/file/coding.txt");
		String result = txt2String(file);
		
		// 测试输出
//		System.out.println(result);
//		System.out.println("--------------------------我是分隔线:)--------------------------------");
		
		// 拼接处理字符串
		String prefix = "<pre clazz="prettyprint linenums">";
		String suffix = "</pre>";
		StringBuffer buffer = new StringBuffer();
		buffer.append(prefix);
		buffer.append(result);
		buffer.append(suffix);
		
		// 输出拼接后的HTML内容,可以直接使用
		System.out.println(buffer.toString());
				
	}
}

即可使用!