r722 - in trunk: docutils/src/main/resources/docutils doxia-module-jrst/src/main/java/org/nuiton/jrst doxia-module-jrst/src/main/resources/META-INF/maven jrst/src/main/java/org/nuiton/jrst jrst/src/test/java/org/nuiton/jrst
Author: jpages Date: 2012-06-18 18:34:48 +0200 (Mon, 18 Jun 2012) New Revision: 722 Url: http://nuiton.org/repositories/revision/jrst/722 Log: Suite du d?\195?\169veloppement de la g?\195?\169n?\195?\169ration pdf dans le module doxia Modified: trunk/docutils/src/main/resources/docutils/__run__.py trunk/doxia-module-jrst/src/main/java/org/nuiton/jrst/JrstSiteRenderer.java trunk/doxia-module-jrst/src/main/java/org/nuiton/jrst/ReflectUtil.java trunk/doxia-module-jrst/src/main/resources/META-INF/maven/RstAggregation.vm trunk/jrst/src/main/java/org/nuiton/jrst/JRST.java trunk/jrst/src/test/java/org/nuiton/jrst/JRSTTest.java Modified: trunk/docutils/src/main/resources/docutils/__run__.py =================================================================== --- trunk/docutils/src/main/resources/docutils/__run__.py 2012-06-14 17:35:04 UTC (rev 721) +++ trunk/docutils/src/main/resources/docutils/__run__.py 2012-06-18 16:34:48 UTC (rev 722) @@ -36,7 +36,7 @@ __builtin__.__dict__["openlegacy"] = __builtin__.__dict__["open"] __builtin__.__dict__["open"] = jaropen -def exec_docutils (docutilsPath, typeOutput, fileList): +def exec_docutils (docutilsPath, typeOutput, fileIn): # Initalization before using Docutils init_docutils(docutilsPath) @@ -55,7 +55,6 @@ listType = ["xml", "html", "odt", "latex", "man", "s5", "xetex"] if typeOutput in listType: from docutils.core import publish_file - for fileIn in fileList: - publish_file( source_path=fileIn, writer_name=typeOutput ) + publish_file( source_path=fileIn, writer_name=typeOutput ) else: print "Wrong output format" Modified: trunk/doxia-module-jrst/src/main/java/org/nuiton/jrst/JrstSiteRenderer.java =================================================================== --- trunk/doxia-module-jrst/src/main/java/org/nuiton/jrst/JrstSiteRenderer.java 2012-06-14 17:35:04 UTC (rev 721) +++ trunk/doxia-module-jrst/src/main/java/org/nuiton/jrst/JrstSiteRenderer.java 2012-06-18 16:34:48 UTC (rev 722) @@ -1,36 +1,36 @@ package org.nuiton.jrst; import java.io.FileNotFoundException; -import java.io.Reader; import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.maven.doxia.sink.render.RenderingContext; +import org.apache.maven.doxia.site.decoration.DecorationModel; +import org.apache.maven.doxia.site.decoration.Menu; +import org.apache.maven.doxia.site.decoration.MenuItem; import org.apache.maven.doxia.siterenderer.DefaultSiteRenderer; import org.apache.maven.doxia.siterenderer.DocumentRenderer; import org.apache.maven.doxia.siterenderer.RendererException; import org.apache.maven.doxia.siterenderer.SiteRenderingContext; -import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink; -import org.apache.maven.doxia.tools.SiteTool; import org.apache.maven.project.MavenProject; +import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.context.Context; import java.io.File; import java.io.IOException; -import java.io.Writer; -import java.lang.reflect.Method; import java.util.Collection; -import java.util.List; import java.util.Locale; import java.util.Map; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.velocity.SiteResourceLoader; import org.codehaus.plexus.velocity.VelocityComponent; - +import org.apache.commons.io.FileUtils; /** * Override of the DefaultSiteRenderer to add pdf generation of the site. * @@ -41,6 +41,14 @@ */ public class JrstSiteRenderer extends DefaultSiteRenderer { + + static public final String DOC_NAME_EN = "JRST documentation"; + static public final String CONTENT_EN = "Table of content"; + static public final String FILE_NAME_EN = "DocumentationJRST.pdf"; + static public final String DOC_NAME_FR = "Documentation JRST"; + static public final String CONTENT_FR = "Sommaire"; + static public final String FILE_NAME_FR = "DocumentationJRST.pdf"; + protected static Log log = LogFactory.getLog(JrstSiteRenderer.class); /** @@ -48,52 +56,12 @@ * * @component */ - protected SiteTool siteTool; - - // this method is the private one from super class - protected Method createContextMethod; - - // this method is the private one from super class - protected Method writeTemplateMethod; - - private String siteSourcesType; - - private List<Locale> locales; - private Locale locale; private MavenProject mavenProject; private boolean pdfGenerationEnabled; - protected Method getWriteTemplateMethod() { - if (writeTemplateMethod == null) { - writeTemplateMethod = ReflectUtil.getMethod( - getClass().getSuperclass(), - "writeTemplate", - new Class<?>[]{ - Writer.class, - Context.class, - SiteRenderingContext.class - }); - } - getLogger(); - return writeTemplateMethod; - } - - protected Method getCreateContextMethod() { - if (createContextMethod == null) { - createContextMethod = ReflectUtil.getMethod( - getClass().getSuperclass(), - "createContext", - new Class<?>[]{ - SiteRendererSink.class, - SiteRenderingContext.class} - ); - } - return createContextMethod; - } - public void render(Collection<DocumentRenderer> documents, SiteRenderingContext siteRenderingContext, File outputDirectory) @@ -105,76 +73,141 @@ Map<String, ?> templateProperties = siteRenderingContext.getTemplateProperties(); + super.render(documents, siteRenderingContext, outputDirectory); + mavenProject = (MavenProject) templateProperties.get("project"); - pdfGenerationEnabled = getBooleanProperty( - (String) mavenProject.getProperties().get("pdfGenerationEnabled")); + pdfGenerationEnabled = true; + /* getBooleanProperty( + (String) mavenProject.getProperties().get("pdfGenerationEnabled")); */ if (pdfGenerationEnabled) { - File file = new File("./main/resources/META-INF/maven/"); - String documentName = "RstAggregation"; - RenderingContext renderingContext = new RenderingContext(file, documentName); - renderDocument(documents, renderingContext, siteRenderingContext); + String absolutePath = outputDirectory.getAbsolutePath(); + try { + renderPdf(documents, siteRenderingContext, absolutePath); + } catch (Exception e) { + log.error("Can't find rst resource to generate pdf documentation", e); + } } - super.render(documents, siteRenderingContext, outputDirectory); } - /** {@inheritDoc} */ - public void renderDocument(Collection<DocumentRenderer> documents, RenderingContext renderingContext, SiteRenderingContext context) + public void renderPdf(Collection<DocumentRenderer> documents, SiteRenderingContext siteRenderingContext, + String outputDirectory) throws RendererException, FileNotFoundException, UnsupportedEncodingException { - SiteRendererSink sink = new SiteRendererSink( renderingContext ); + Context vc = new VelocityContext(); - File doc = new File( renderingContext.getBasedir(), renderingContext.getInputName() ); + vc.put( "locale", locale ); + vc.put( "supportedLocales", Collections.unmodifiableList(siteRenderingContext.getSiteLocales()) ); - Reader reader = null; try { - String resource = doc.getAbsolutePath(); + StringWriter sw = new StringWriter(); + Field velocityField = ReflectUtil.getField(DefaultSiteRenderer.class, "velocity"); - if ( renderingContext.getAttribute( "velocity" ) != null ) - { - try { - SiteResourceLoader.setResource(resource); + VelocityComponent velocity = (VelocityComponent)velocityField.get(this); + VelocityEngine engine = velocity.getEngine(); + String inputEncoding = siteRenderingContext.getInputEncoding(); - createContextMethod = getCreateContextMethod(); - Context vc = ReflectUtil.invokeMethod(createContextMethod, this, sink, context); + // Set velocity variables + String tableOfContent = CONTENT_EN; + String docName = DOC_NAME_EN; + String filename = FILE_NAME_EN; + if (locale.equals(Locale.FRENCH)) { + docName = DOC_NAME_FR; + tableOfContent = CONTENT_FR; + filename = FILE_NAME_FR; + } - StringWriter sw = new StringWriter(); - Field velocityField = ReflectUtil.getField(DefaultSiteRenderer.class, "velocity"); + String titleDecoration = StringUtils.rightPad("", docName.length(), '='); + vc.put("titleDecoration", titleDecoration); + vc.put( "docName", docName); + vc.put( "tableContentName", tableOfContent); - VelocityComponent velocity = (VelocityComponent)velocityField.get(this); - VelocityEngine engine = velocity.getEngine(); - String inputEncoding = context.getInputEncoding(); + // Build the list of item's href + DecorationModel decoration = siteRenderingContext.getDecoration(); + List<Menu> menus = decoration.getMenus(); + Collection<String> paths = new LinkedList<String>(); + for (Menu menu : menus) { + List<MenuItem> items = menu.getItems(); + for (MenuItem item : items) { + buildListPaths(item, paths, siteRenderingContext); + } + } - String chaine = ""; - for (DocumentRenderer document : documents) { - RenderingContext renderingContextDoc = document.getRenderingContext(); - String inputName = renderingContextDoc.getInputName(); + // Build the map of html documents from .rst or .rst.vm files + //HashMap<String, String> map = new HashMap<String, String>(); + LinkedList<String> htmlFiles = new LinkedList<String>(); + Iterator<DocumentRenderer> iterator = documents.iterator(); + while (iterator.hasNext()) { + DocumentRenderer doc = iterator.next(); + RenderingContext renderingContext = doc.getRenderingContext(); + String inputName = renderingContext.getInputName(); + if (inputName.endsWith("rst") || inputName.endsWith("rst.vm")) { + String[] splitName = inputName.split("\\."); + String htmlName = splitName[0] + "." + "html"; + htmlFiles.add(htmlName); + } + } - chaine += ".. include:: " + inputName + "\n\n"; + // Merge of the map of html file and the list of paths + List<String> documentRefs = new LinkedList<String>(); + for (String path: paths) { + for (String file : htmlFiles) { + if (file.equals(path)) { + documentRefs.add(file); + } + } + } + if (documentRefs.size() > 0) { + vc.put("documentRefs", documentRefs); - vc.put("content", chaine); + // Aggregate all rst file info one file + engine.mergeTemplate("/META-INF/maven/RstAggregation.vm", inputEncoding, vc, sw); + String fileContent = sw.toString(); + String tempFilePath = outputDirectory + File.separatorChar + "pdfDoc.rst"; + File pdfDoc = new File(tempFilePath); + FileUtils.writeStringToFile(pdfDoc, fileContent, "UTF-8"); + String outputFilePath = outputDirectory + File.separatorChar + filename; - engine.mergeTemplate(resource, inputEncoding, vc, sw); + // Define the output file + File fileOut = new File(outputFilePath); + JRST.Overwrite alltime = JRST.Overwrite.ALLTIME; - } catch ( Exception e ) { - if (getLogger().isDebugEnabled()) { - getLogger().error( "Error parsing " + resource + " as a velocity template, using as text.", e ); - } else { - getLogger().error( "Error parsing " + resource + " as a velocity template, using as text." ); - } - } + // Generate the pdf file + JRST.generate("pdf", pdfDoc, fileOut, alltime, false); } + } catch (Exception e) { - log.error("", e); - } finally { - sink.flush(); - sink.close(); - IOUtil.close(reader); + log.error("Can't generate pdf documentation of the project", e); } } + public Collection<String> buildListPaths(MenuItem item, Collection<String> paths, + SiteRenderingContext siteRenderingContext) { + // add hrefs to paths if they don't start with "http" or ".." + String href = item.getHref(); + if (!href.startsWith("http") && !href.startsWith("..")) { + boolean addHref = true; + for (Locale loc : siteRenderingContext.getSiteLocales()) { + if (href.startsWith(loc.toString())) { + addHref = false; + } + } + if (addHref) { + paths.add(href); + } + } + // If the item has sub-items, we use recursion to add them to paths + List<MenuItem> subItems = item.getItems(); + if (!subItems.isEmpty()) { + for (MenuItem subItem : subItems) { + paths = buildListPaths(subItem, paths, siteRenderingContext); + } + } + return paths; + } + protected boolean getBooleanProperty(String value) { boolean result = StringUtils.isNotEmpty(value) && !"false".equals(value); return result; Modified: trunk/doxia-module-jrst/src/main/java/org/nuiton/jrst/ReflectUtil.java =================================================================== --- trunk/doxia-module-jrst/src/main/java/org/nuiton/jrst/ReflectUtil.java 2012-06-14 17:35:04 UTC (rev 721) +++ trunk/doxia-module-jrst/src/main/java/org/nuiton/jrst/ReflectUtil.java 2012-06-18 16:34:48 UTC (rev 722) @@ -49,7 +49,7 @@ public static Field getField(Class<?> klass, String fieldName) { try { - Field declaredField = klass.getField(fieldName); + Field declaredField = klass.getDeclaredField(fieldName); declaredField.setAccessible(true); return declaredField; } catch (Exception e) { Modified: trunk/doxia-module-jrst/src/main/resources/META-INF/maven/RstAggregation.vm =================================================================== --- trunk/doxia-module-jrst/src/main/resources/META-INF/maven/RstAggregation.vm 2012-06-14 17:35:04 UTC (rev 721) +++ trunk/doxia-module-jrst/src/main/resources/META-INF/maven/RstAggregation.vm 2012-06-18 16:34:48 UTC (rev 722) @@ -23,8 +23,13 @@ .. * #L% .. - -=================================== -Parseur reStructuredText_ : le JRst -=================================== +$titleDecoration +$docName +$titleDecoration -$content \ No newline at end of file +.. contents:: $tableContentName + + +#foreach($document in $documents) +.. include:: $documentRefs +#end \ No newline at end of file Modified: trunk/jrst/src/main/java/org/nuiton/jrst/JRST.java =================================================================== --- trunk/jrst/src/main/java/org/nuiton/jrst/JRST.java 2012-06-14 17:35:04 UTC (rev 721) +++ trunk/jrst/src/main/java/org/nuiton/jrst/JRST.java 2012-06-18 16:34:48 UTC (rev 722) @@ -360,7 +360,7 @@ interp.setOut(out); // Execution of the docutils script to transform rst to xml - String commandExec = String.format("__run__.exec_docutils('%s', '%s', ['%s'])", + String commandExec = String.format("__run__.exec_docutils('%s', '%s', '%s')", docutilsPath, TYPE_XML, filePath); interp.exec(commandExec); @@ -385,68 +385,6 @@ } } - public static void generateDocutilsList(List<File> listIn) throws Exception { - - ByteArrayOutputStream out = null; - - try { - // Transformation to XML - out = new ByteArrayOutputStream(); - - // Transformation of the __run__ URL into a path that python will use - // For example the URL is : - // jar:file:/home/user/.m2/repository/org/nuiton/jrst/docutils/1.6-SNAPSHOT/docutils-1.6-SNAPSHOT.jar!/__run__.py - // and it becomes : - // /home/user/.m2/repository/org/nuiton/jrst/docutils/1.6-SNAPSHOT/docutils-1.6-SNAPSHOT.jar/ - URL resource = JRST.class.getResource("/" + DOCUTILS_LAUNCHER); - String docutilsPath = resource.getPath() - .replaceAll(DOCUTILS_LAUNCHER, ""); - - docutilsPath = docutilsPath.replaceAll(BANG, ""); - docutilsPath = docutilsPath.replaceAll(FILE_URI_PREFIX, ""); - - // Import of the main script to use docutils ( __run__ ) - PythonInterpreter interp = new PythonInterpreter(); - String commandImport = IMPORT_SCRIPT; - interp.exec(commandImport); - - /* - // If the OS is windows, escapes the backslashs in the filepath - String filePath = in.getAbsolutePath(); - String property = System.getProperty(OS_NAME).toLowerCase(); - if (property.contains(WINDOWS_NAME)) { - filePath = filePath.replaceAll("\\\\", "\\\\\\\\"); - } - */ - - // Sets an output stream in the python interpreter and executes the code - interp.setOut(out); - - // Execution of the docutils script to transform rst to xml - String commandExec = String.format("__run__.exec_docutils('%s', '%s', [",docutilsPath, TYPE_XML); - - for (File aListIn : listIn) { - commandExec += "'" + aListIn.getAbsolutePath() + "',"; - } - - commandExec = commandExec.substring(0,commandExec.lastIndexOf(",")); - commandExec += "])"; - log.info(commandExec); - interp.exec(commandExec); - - // Cleans the python interpreter to avoid problems if they are multiple execution of this method - interp.cleanup(); - - // Transforms the output stream to a document - String xmlString = new String(out.toByteArray(), UTF_8); - log.info(xmlString); - } finally { - if (out != null) { - out.close(); - } - } - } - /** * Applies XSL stylesheet(s) to a XML document * Modified: trunk/jrst/src/test/java/org/nuiton/jrst/JRSTTest.java =================================================================== --- trunk/jrst/src/test/java/org/nuiton/jrst/JRSTTest.java 2012-06-14 17:35:04 UTC (rev 721) +++ trunk/jrst/src/test/java/org/nuiton/jrst/JRSTTest.java 2012-06-18 16:34:48 UTC (rev 722) @@ -70,35 +70,6 @@ } @Test - public void generateMultipleXml() throws Exception { - - File in = getTestFile("test.rst"); - File out = getOutputTestFile("jrst-RstToXml.xml"); - - List<File> liste = new LinkedList<File>(); - for (int i = 0; i < 1 ; i++) { - liste.add(in) ; - } - - TimeLog docutilsTimeLog = new TimeLog("Generation"); - - long start = TimeLog.getTime(); - JRST.generateDocutilsList(liste); - - start = docutilsTimeLog.log(start, "generation avec boucle Python"); - - for (int i = 0; i < 1 ; i++) { - JRST.generateDocutils(in); - } - start = docutilsTimeLog.log(start, "multiple generation normale"); - - for (int i = 0; i < 1 ; i++) { - JRST.generateSimpleDoc(in, "UTF-8"); - } - docutilsTimeLog.log(start, "multiple generation simple"); - } - - @Test public void generateSimpleXml() throws Exception { File in = getTestFile("test.rst");
participants (1)
-
jpages@users.nuiton.org