Skip to content

Commit

Permalink
Code-prettify is local.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbellus committed Mar 6, 2017
1 parent f26e3a1 commit 36f01fe
Show file tree
Hide file tree
Showing 65 changed files with 96 additions and 1,158 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Overview

The project extends [Fitnesse](http://www.fitnesse.org/) wiki with colorized code.
It uses [code-prettify](https://github.com/google/code-prettify).
It uses [code-prettify](https://github.com/google/code-prettify). The [code-prettify](https://github.com/google/code-prettify) is embedded in jar file. When plugin is loaded it copies [code-prettify](https://github.com/google/code-prettify) into FitNesseRoot/files/fitnesse to be accessible by html pages. The [code-prettify](https://github.com/google/code-prettify) is modified to use style ```cptag``` instead of ```tag``` due to FitNesse style ```tag```.

# Installation

Expand All @@ -16,7 +16,9 @@ No Configuration.

After installation and Fitnesse restart you should be able to use command on wiki
```
!listing{
!listing [language] {
some code
}
```
The attribute ```laguage``` is optional. Can be one of ```bsh|c|cc|cpp|cs|csh|cyc|cv|htm|html|java|js|m|mxml|perl|pl|pm|py|rb|sh|xhtml|xml|xsl|json```.
When ```xml``` or ```json``` is used then plugin also idents given text.
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.github.sbellus.fitnesse.codeprettifier;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.commons.io.FilenameUtils;
import org.json.JSONObject;

import fitnesse.html.HtmlTag;
Expand All @@ -25,65 +33,115 @@

public class ListingSymbol extends SymbolType implements Rule, Translation {
private static final Pattern OptionsPattern = Pattern.compile("!listing[ \t]+([a-z]*)[ \t]*.*");

public static SymbolType make() {
return new ListingSymbol();
}

public ListingSymbol() {
super("Listing");
wikiMatcher(new Matcher().string("!listing").endsWith(new char[] {'(', '{', '['}));
wikiMatcher(new Matcher().string("!listing").endsWith(new char[] { '(', '{', '[' }));
wikiRule(this);
htmlTranslation(this);

try {
// get output folder
File currentJarFile = new File(CodePrettifierPlugin.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
String currentJarPath = FilenameUtils.getFullPath(currentJarFile.getAbsolutePath());
String outputFolder = currentJarPath.replace("plugins", "FitNesseRoot" + File.separator + "files" + File.separator + "fitnesse");

System.out.println("Listing Plugin code-prettify folder : " + outputFolder);

new File(outputFolder).mkdirs();

// unpack code prettify scripts
byte[] buffer = new byte[1024];

InputStream is = getClass().getClassLoader().getResourceAsStream("code-prettify.zip");
ZipInputStream zis = new ZipInputStream(is);
// get the zipped file list entry
ZipEntry ze;

ze = zis.getNextEntry();

while (ze != null) {

if (ze.isDirectory())
{
ze = zis.getNextEntry();
continue;
}

String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);

// create all non exists folders
// else you will hit FileNotFoundException for compressed folder
new File(newFile.getParent()).mkdirs();

FileOutputStream fos = new FileOutputStream(newFile);

int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}

fos.close();
ze = zis.getNextEntry();
}

zis.closeEntry();
zis.close();

} catch (URISyntaxException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public Maybe<Symbol> parse(Symbol current, Parser parser) {

String content = current.getContent();
// check keyword store in content again more strictly
if (! (content.charAt(8) == ' ' || content.charAt(8) == '(' || content.charAt(8) == '{' || content.charAt(8) == '['))
{
if (!(content.charAt(8) == ' ' || content.charAt(8) == '(' || content.charAt(8) == '{'
|| content.charAt(8) == '[')) {
// not my symbol. I expect "!listing ".
return Symbol.nothing;
}

// get options
java.util.regex.Matcher m = OptionsPattern.matcher(content);
String type = "";
if (m.find())
{
if (m.find()) {
type = m.group(1);
}

// get content
char beginner = content.charAt(content.length() - 1);
content = parser.parseLiteral(closeType(beginner));
if (parser.atEnd())
{
if (parser.atEnd()) {
return Symbol.nothing;
}

current.putProperty("type", type);

if (type.equals("json"))
{

if (type.equals("json")) {
content = IdentJson(content);
}
else if (type.equals("xml"))
{
} else if (type.equals("xml")) {
content = IdentXml(content);
}

current.putProperty("content", content);

return new Maybe<Symbol>(current);
}

private String IdentXml(String content) {
String identedXml = content;

try {

Source xmlInput = new StreamSource(new StringReader(trim(content)));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
Expand All @@ -99,13 +157,13 @@ private String IdentXml(String content) {
// this is case of wrong xml
identedXml += "\n<!--XML has error : " + e.getMessage() + " --!>";
}

return identedXml;
}

private String IdentJson(String content) {
String identedJson = content;

try {
JSONObject json = new JSONObject(content); // Convert text to object
identedJson = json.toString(4);
Expand All @@ -127,35 +185,34 @@ private String trim(String input) {
StringBuffer result = new StringBuffer();
try {
String line;
while ( (line = reader.readLine() ) != null)
while ((line = reader.readLine()) != null)
result.append(line.trim());
return result.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public String toTarget(Translator translator, Symbol symbol) {

HtmlTag listingSection = new HtmlTag("div");
String divclassValue = "listing";
if (! symbol.getProperty("type").isEmpty())
{
divclassValue += "_" + symbol.getProperty("type");
if (!symbol.getProperty("type").isEmpty()) {
divclassValue += "_" + symbol.getProperty("type");
}
listingSection.addAttribute("class", divclassValue);
listingSection.addAttribute("type", symbol.getProperty("type"));
String originalContent = trim(symbol.getProperty("content"));
originalContent = originalContent.replaceAll("&", "&amp;");
originalContent = originalContent.replaceAll("\"", "&quot;");
listingSection.addAttribute("originalcontent", originalContent);
listingSection.add(new RawHtml("<script src=\"/files/fitnesse/code-prettify/run_prettify.js\" type=\"text/javascript\"></script>"));
listingSection.add(new RawHtml(
"<script src=\"/files/fitnesse/code-prettify/run_prettify.js\" type=\"text/javascript\"></script>"));
HtmlTag pre = new HtmlTag("pre");

String prettyprintclass = "prettyprint";
if (! symbol.getProperty("type").isEmpty())
{
prettyprintclass += " lang-" + symbol.getProperty("type");
if (!symbol.getProperty("type").isEmpty()) {
prettyprintclass += " lang-" + symbol.getProperty("type");
}
pre.addAttribute("class", prettyprintclass);
pre.add(new HtmlText(symbol.getProperty("content")));
Expand Down
Binary file added src/main/resources/code-prettify.zip
Binary file not shown.
18 changes: 0 additions & 18 deletions src/main/resources/code-prettify/lang-aea.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/resources/code-prettify/lang-agc.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/resources/code-prettify/lang-apollo.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/resources/code-prettify/lang-basic.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/resources/code-prettify/lang-cbm.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/main/resources/code-prettify/lang-cl.js

This file was deleted.

17 changes: 0 additions & 17 deletions src/main/resources/code-prettify/lang-clj.js

This file was deleted.

Loading

0 comments on commit 36f01fe

Please sign in to comment.