Skip to content

Commit

Permalink
Merge pull request #165 from metanorma/sourcecode_highlight
Browse files Browse the repository at this point in the history
Sourcecode highlight
  • Loading branch information
Intelligent2013 authored Dec 17, 2022
2 parents 8d86845 + 3160e3a commit fb46458
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SHELL ?= /bin/bash
endif

#JAR_VERSION := $(shell mvn -q -Dexec.executable="echo" -Dexec.args='$${project.version}' --non-recursive exec:exec -DforceStdout)
JAR_VERSION := 1.55
JAR_VERSION := 1.56
JAR_FILE := mn2pdf-$(JAR_VERSION).jar

all: target/$(JAR_FILE)
Expand Down
10 changes: 5 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ You will need the Java Development Kit (JDK) version 8, Update 241 (8u241) or hi

[source,sh]
----
java -Xss5m -Xmx2048m -jar target/mn2pdf-1.55.jar --xml-file <XML-FileName> --xsl-file <XSLT-FileName> --pdf-file <Output-PDF-FileName> [--syntax-highlight]
java -Xss5m -Xmx2048m -jar target/mn2pdf-1.56.jar --xml-file <XML-FileName> --xsl-file <XSLT-FileName> --pdf-file <Output-PDF-FileName> [--syntax-highlight]
----

e.g.

[source,sh]
----
java -Xss5m -Xmx2048m -jar target/mn2pdf-1.55.jar --xml-file tests/G.191.xml --xsl-file tests/itu.recommendation.xsl --pdf-file tests/G.191.pdf
java -Xss5m -Xmx2048m -jar target/mn2pdf-1.56.jar --xml-file tests/G.191.xml --xsl-file tests/itu.recommendation.xsl --pdf-file tests/G.191.pdf
----

=== PDF encryption features
Expand Down Expand Up @@ -100,7 +100,7 @@ Update version in `pom.xml`, e.g.:
----
<groupId>org.metanorma.fop</groupId>
<artifactId>mn2pdf</artifactId>
<version>1.55</version>
<version>1.56</version>
<name>Metanorma XML to PDF converter</name>
----

Expand All @@ -111,8 +111,8 @@ Tag the same version in Git:

[source,xml]
----
git tag v1.55
git push origin v1.55
git tag v1.56
git push origin v1.56
----

Then the corresponding GitHub release will be automatically created at:
Expand Down
8 changes: 6 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.metanorma.fop</groupId>
<artifactId>mn2pdf</artifactId>
<version>1.55</version>
<version>1.56</version>
<name>Metanorma XML to PDF converter</name>
<packaging>jar</packaging>
<url>https://www.metanorma.org</url>
Expand Down Expand Up @@ -237,6 +237,10 @@
<artifactId>js-scriptengine</artifactId>
<version>21.1.0</version>
</dependency>

<dependency>
<groupId>net.sourceforge.cssparser</groupId>
<artifactId>cssparser</artifactId>
<version>0.9.29</version>
</dependency>
</dependencies>
</project>
75 changes: 75 additions & 0 deletions src/main/java/org/metanorma/fop/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import javax.imageio.stream.ImageInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.xpath.XPath;
Expand All @@ -70,14 +71,21 @@
import javax.xml.xpath.XPathFactory;
import static org.metanorma.Constants.DEBUG;
import static org.metanorma.fop.SourceXMLDocument.tmpfilepath;

import com.steadystate.css.dom.CSSStyleRuleImpl;
import com.steadystate.css.parser.CSSOMParser;
import com.steadystate.css.parser.SACParserCSS3;
import org.metanorma.fop.fonts.FOPFont;
import org.metanorma.utils.LoggerHelper;
import org.w3c.css.sac.SelectorList;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.css.*;
import org.xml.sax.InputSource;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.SAXException;

/**
*
Expand Down Expand Up @@ -708,4 +716,71 @@ public static String encodeBase64(String input) {
}
return Base64.getEncoder().encodeToString(input.getBytes());
}

public static Node parseCSS(String cssString) {
StringBuilder sbCSSxml = new StringBuilder();
try {
sbCSSxml.append("<css>");
org.w3c.css.sac.InputSource source = new org.w3c.css.sac.InputSource(new StringReader(cssString));
CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
CSSStyleSheet sheet = parser.parseStyleSheet(source, null, null);

CSSRuleList rules = sheet.getCssRules();
for (int i = 0; i < rules.getLength(); i++) {
final CSSRule rule = rules.item(i);

if (rule instanceof CSSStyleRule) {
CSSStyleRule styleRule = (CSSStyleRule) rule;
SelectorList selectorList = ((CSSStyleRuleImpl) rule).getSelectors();

//System.out.println("selector: " + styleRule.getSelectorText());
CSSStyleDeclaration styleDeclaration = styleRule.getStyle();
StringBuilder properties = new StringBuilder();
for (int j = 0; j < styleDeclaration.getLength(); j++) {
String property = styleDeclaration.item(j);
String value = styleDeclaration.getPropertyCSSValue(property).getCssText();
//System.out.println("property: " + property);
//System.out.println("value: " + value);
properties.append("<property name=\"");
properties.append(property);
properties.append("\" value=\"");
properties.append(value);
properties.append("\"/>");
}

for (int s = 0; s < selectorList.getLength(); s++) {
String selectorText = selectorList.item(s).toString();
if (selectorText.contains(".")) {
selectorText = selectorText.substring(selectorText.lastIndexOf(".") + 1).trim();
} else if (selectorText.contains(" ")) {
selectorText = selectorText.substring(selectorText.lastIndexOf(" ") + 1).trim();
}
//selectorText = selectorText.replaceAll("sourcecode \\.","");
//System.out.println("selector: " + selectorText);
sbCSSxml.append("<class name=\"");
sbCSSxml.append(selectorText);
sbCSSxml.append("\">");
sbCSSxml.append(properties);
sbCSSxml.append("</class>");
}
}
}
sbCSSxml.append("</css>");

} catch (IOException e) {
logger.severe("CSS parsing error: " + e.toString());
sbCSSxml.setLength(0);
sbCSSxml.append("<css></css>");
}
Node node = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document parsed = builder.parse(new InputSource(new StringReader(sbCSSxml.toString())));
node = parsed.getDocumentElement();
} catch (IOException | ParserConfigurationException | SAXException e) {
logger.severe("CSS parsing error: " + e.toString());
}
return node;
}
}
1 change: 1 addition & 0 deletions src/main/resources/tables_only.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
</xsl:choose>
</xsl:template>

<xsl:template match="*[local-name() = 'table'][@type = 'sourcecode']" priority="2"/>

<xsl:template match="@*|node()" mode="simple_td">
<xsl:copy>
Expand Down
43 changes: 34 additions & 9 deletions src/test/java/org/metanorma/fop/mn2pdfTests.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package org.metanorma.fop;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Calendar;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import com.steadystate.css.dom.CSSStyleRuleImpl;
import com.steadystate.css.parser.CSSOMParser;
import com.steadystate.css.parser.SACParserCSS3;
import org.apache.commons.cli.ParseException;
import org.apache.pdfbox.cos.COSName;

Expand All @@ -33,6 +31,8 @@
import org.apache.pdfbox.text.PDFTextStripper;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.BeforeClass;
Expand All @@ -45,7 +45,11 @@
import static org.metanorma.Constants.ERROR_EXIT_CODE;
import static org.metanorma.fop.PDFGenerator.logger;
import org.metanorma.utils.LoggerHelper;
import org.w3c.css.sac.InputSource;
import org.w3c.css.sac.Selector;
import org.w3c.css.sac.SelectorList;
import org.w3c.dom.Node;
import org.w3c.dom.css.*;

public class mn2pdfTests {

Expand Down Expand Up @@ -399,6 +403,27 @@ public void checkSpacesInPDF() throws ParseException {
assertTrue(pdftext.contains("the_integers") && pdftext.contains("elementary_space") && pdftext.contains("make_elementary_space"));
}

@Test
public void checkCSSparsing() throws IOException {
String cssString = "sourcecode .c, sourcecode .ch {\n" +
" color: #FF0000;\n" +
"}";
Node xmlNode = Util.parseCSS(cssString);
String xmlStr = nodeToString(xmlNode);
assertEquals("<css><class name=\"c\"><property name=\"color\" value=\"rgb(255, 0, 0)\"/></class><class name=\"ch\"><property name=\"color\" value=\"rgb(255, 0, 0)\"/></class></css>", xmlStr);
}

private static String nodeToString(Node node) {
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
System.out.println("nodeToString Transformer Exception");
}
return sw.toString();
}


@Test
Expand Down

0 comments on commit fb46458

Please sign in to comment.