Skip to content

Commit

Permalink
finish decoupling JavaHelpHtmlWriter - gives same output
Browse files Browse the repository at this point in the history
  • Loading branch information
jcschaff committed May 4, 2024
1 parent 65b63ba commit 095a5bf
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.vcell.documentation;

public interface DocReferenceTarget {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Paths;
import java.util.*;

public class DocumentCompiler {
Expand All @@ -29,8 +28,8 @@ public class DocumentCompiler {
private final static int maxImgHeight = 600;
private final static String imageFilePath = "topics/image/";
private final static String tocFileName = "TOC.xml";
private final static String definitionFilePath = "topics/ch_9/Appendix/";
private final static String definitionXMLFileName = "Definitions.xml";
public final static String definitionTarget = "Definitions";

private final File docTargetDir;
private final File docSourceDir;
Expand Down Expand Up @@ -63,19 +62,11 @@ public static void main(String[] args) {
throw new RuntimeException("document target directory "+docTargetDir.getPath()+" isn't a directory");
}
DocumentCompiler docCompiler = new DocumentCompiler(docSourceDir, docTargetDir);
DocumentWriter documentWriter = new HtmlWriter(docSourceDir, docTargetDir, definitionXMLFileName, definitionFilePath, tocFileName);
Documentation documentation = docCompiler.parseDocumentation();

DocumentWriter documentWriter = new JavaHelpHtmlWriter(docSourceDir, docTargetDir, tocFileName);
documentWriter.writeFiles(documentation);
//write document definitions
documentWriter.writeDefinitions(documentation);
//write document pages
documentWriter.writePages(documentation);

documentWriter.generateHelpMap(documentation);
documentWriter.processTOC(documentation);

documentWriter.copyHelpSet();
documentWriter.generateHelpSearch();
}catch (Throwable e){
e.printStackTrace(System.out);
}
Expand Down Expand Up @@ -216,9 +207,8 @@ private void readTOCItem(Documentation documentation, HashSet<DocumentPage> page
if (target!=null){
// first look for a documentPage as the target, else check if it is a Definition page.
DocumentPage targetDocPage = documentation.getDocumentPage(new DocLink(target,target));
String definitionPageTarget = definitionXMLFileName.replace(".xml","");
if (targetDocPage==null){
if (!target.equals(definitionPageTarget)){
if (!target.equals(definitionTarget)){
throw new RuntimeException("table of contents referencing nonexistant target '"+target+"'");
}
}else{
Expand Down Expand Up @@ -346,8 +336,4 @@ private void readBlock(DocTextComponent docComponent, Element element, File xmlF
}
}
}

private String getHelpRelativePath(File sourceDir, File targetFile) {
return Paths.get(sourceDir.getPath()).relativize(Paths.get(targetFile.getPath())).toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import java.io.File;

public class DocumentDefinition {
public class DocumentDefinition implements DocReferenceTarget {
private File sourceFile;
private String target;
private String label;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import java.io.File;

public class DocumentImage {
public class DocumentImage implements DocReferenceTarget {
private File sourceFile;
private String target;
private int fileWidth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import java.io.File;

public class DocumentPage {
public class DocumentPage implements DocReferenceTarget {
private File templateFile;
private String title;
private String target;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
package org.vcell.documentation;

import org.jdom.Element;

import java.io.IOException;

public interface DocumentWriter {
void writePages(Documentation documentation);

void writeDefinitions(Documentation documentation);

void processTOC(Documentation documentation) throws Exception;

void generateHelpMap(Documentation documentation) throws Exception;

void copyHelpSet() throws IOException;
void writeFiles(Documentation documentation) throws Exception;

void generateHelpSearch() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Documentation {
private ArrayList<DocumentPage> documentPages = new ArrayList<DocumentPage>();
private ArrayList<DocumentImage> documentImages = new ArrayList<DocumentImage>();
private ArrayList<DocumentDefinition> documentDefinitions = new ArrayList<DocumentDefinition>();
private HashSet<Object> referencedTargets = new HashSet<Object>();
private HashSet<DocReferenceTarget> referencedTargets = new HashSet<>();

public DocumentPage getDocumentPage(DocLink docLink) {
for (DocumentPage docPage : documentPages){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,26 @@
import java.util.List;


public class HtmlWriter implements DocumentWriter {
public class JavaHelpHtmlWriter implements DocumentWriter {
private final static String tocHTMLFileName = "VCellHelpTOC.html";
private final static String helpSetFileName = "HelpSet.hs";
private final static String javaHelp_helpSearchConfigFile = "helpSearchConfig.txt";
private final static String helpSearchFolderName = "JavaHelpSearch";
private final static String mapFileName = "Map.jhm";

private final static String definitionFilePath = "topics/ch_9/Appendix/";


private final File docSourceDir;
private final File docTargetDir;
private final String definitionXMLFileName;
private final String definitionFilePath;
private final String tocFileName;

public HtmlWriter(File docSourceDir, File docTargetDir, String definitionXMLFileName, String definitionFilePath, String tocFileName) {
public JavaHelpHtmlWriter(File docSourceDir, File docTargetDir, String tocFileName) {
this.docSourceDir = docSourceDir;
this.docTargetDir = docTargetDir;
this.definitionXMLFileName = definitionXMLFileName;
this.definitionFilePath = definitionFilePath;
this.tocFileName = tocFileName;
}

public void writePages(Documentation documentation) {
private void writePages(Documentation documentation) {
for(DocumentPage documentPage : documentation.getDocumentPages()) {
File htmlFile = getTargetFile(documentPage.getTemplateFile());
htmlFile = new File(htmlFile.getPath().replace(".xml",".html"));
Expand All @@ -53,7 +49,7 @@ public void writePages(Documentation documentation) {
}
}

public void writeHTML(Documentation documentation, DocumentPage documentPage, File htmlFile) throws Exception
private void writeHTML(Documentation documentation, DocumentPage documentPage, File htmlFile) throws Exception
{
try (PrintWriter pw = new PrintWriter(htmlFile)) {
//start html
Expand Down Expand Up @@ -113,7 +109,7 @@ public void writeHTML(Documentation documentation, DocumentPage documentPage, Fi
}
}

public void printComponent(Documentation documentation, DocTextComponent docComp, File directory, PrintWriter pw, File sourceHtmlFile) throws Exception {
private void printComponent(Documentation documentation, DocTextComponent docComp, File directory, PrintWriter pw, File sourceHtmlFile) throws Exception {
if (docComp instanceof DocText text){
if (text.getBold()) {
pw.print("<b>" + text.getText() + "</b>");
Expand All @@ -136,11 +132,10 @@ public void printComponent(Documentation documentation, DocTextComponent docComp
{
DocumentPage docPage = documentation.getDocumentPage(docLink);
if (docPage==null){
if(docLink.getTarget().equals(definitionXMLFileName.replace(".xml", "")))
if(docLink.getTarget().equals(DocumentCompiler.definitionTarget))
{
File workingDefinitionDir= new File(docTargetDir, definitionFilePath);
File htmlFile = new File(workingDefinitionDir, definitionXMLFileName);
htmlFile = new File(htmlFile.getPath().replace(".xml",".html"));
File htmlFile = new File(workingDefinitionDir, DocumentCompiler.definitionTarget+".html");
String relativePathToTarget = getHelpRelativePath(directory, htmlFile);
pw.print("<a href=\""+relativePathToTarget+"\">");
}
Expand Down Expand Up @@ -206,15 +201,7 @@ public void printComponent(Documentation documentation, DocTextComponent docComp
}
}

public void buildHtmlIndex(Documentation documentation, Element rootElement) throws IOException {
File htmlTOCFile=new File(docTargetDir,tocHTMLFileName);
PrintWriter tocPrintWriter = new PrintWriter(htmlTOCFile);
buildIndexHtml(documentation, rootElement,0,tocPrintWriter);
tocPrintWriter.close();
}

@Override
public void writeDefinitions(Documentation documentation) {
private void writeDefinitions(Documentation documentation) {
if(documentation.getDocumentDefinitions() != null && documentation.getDocumentDefinitions().length > 0)
{
DocumentDefinition[] documentDefinitions = documentation.getDocumentDefinitions();
Expand All @@ -230,12 +217,16 @@ public void writeDefinitions(Documentation documentation) {

}

@Override
public void copyHelpSet() throws IOException {
private void copyHelpSet() throws IOException {
//FileUtils.copyFile(new File(docSourceDir, helpSetFileName),new File(docTargetDir, helpSetFileName));
FileUtils.copyFile(new File(docSourceDir, helpSetFileName), new File(docTargetDir, helpSetFileName), true, true, 4 * 1024);
}

private void copyTableOfContents() throws IOException {
// copy the Table of Contents unchanged from the source to the target directory.
FileUtils.copyFile(new File(docSourceDir, tocFileName),new File(docTargetDir, tocFileName));
}

private void writeDefinitionHTML(DocumentDefinition[] docDefs, File htmlFile) throws Exception
{
try (PrintWriter pw = new PrintWriter(htmlFile)) {
Expand Down Expand Up @@ -272,8 +263,7 @@ private void writeDefinitionHTML(DocumentDefinition[] docDefs, File htmlFile) th
}
}

@Override
public void generateHelpMap(Documentation documentation) throws Exception
private void generateHelpMap(Documentation documentation) throws Exception
{
File mapFile = new File(docTargetDir, mapFileName);

Expand All @@ -297,8 +287,8 @@ public void generateHelpMap(Documentation documentation) throws Exception
}
//add definitions to map
Element definitionElement = new Element(VCellDocTags.mapID_tag);
definitionElement.setAttribute(VCellDocTags.target_attr, definitionXMLFileName.replace(".xml",""));
definitionElement.setAttribute(VCellDocTags.url_attr, definitionFilePath /*+ File.separator*/ + definitionXMLFileName.replace(".xml", ".html"));
definitionElement.setAttribute(VCellDocTags.target_attr, DocumentCompiler.definitionTarget);
definitionElement.setAttribute(VCellDocTags.url_attr, definitionFilePath /*+ File.separator*/ + DocumentCompiler.definitionTarget+".html");
mapElement.addContent(definitionElement);
//convert mapdocument to string
Document mapDoc = new Document();
Expand All @@ -315,8 +305,7 @@ public void generateHelpMap(Documentation documentation) throws Exception
}


@Override
public void processTOC(Documentation documentation) throws Exception
private void generateWebIndexHtml(Documentation documentation) throws Exception
{
File tocSourceFile = new File(docSourceDir, tocFileName);
//
Expand All @@ -328,12 +317,14 @@ public void processTOC(Documentation documentation) throws Exception
throw new RuntimeException("expecting "+VCellDocTags.toc_tag+" in file "+tocSourceFile.getPath());
}

// copy the Table of Contents to the target directory.
FileUtils.copyFile(new File(docSourceDir, tocFileName),new File(docTargetDir, tocFileName));
//System.out.println("Calling buildHtmlIndex");
buildHtmlIndex(documentation, root);
File htmlTOCFile=new File(docTargetDir,tocHTMLFileName);
PrintWriter tocPrintWriter = new PrintWriter(htmlTOCFile);
buildIndexHtml(documentation, root,0,tocPrintWriter);
tocPrintWriter.close();
}


private void buildIndexHtml(Documentation documentation, Element element, int level,PrintWriter tocPrintWriter) {
if (element.getName().equals(VCellDocTags.tocitem_tag)){
String target = element.getAttributeValue(VCellDocTags.target_attr);
Expand Down Expand Up @@ -368,8 +359,7 @@ private void buildIndexHtml(Documentation documentation, Element element, int le
}


@Override
public void generateHelpSearch() throws Exception {
private void generateHelpSearch() throws Exception {
File helpSearchDir = new File(docTargetDir, helpSearchFolderName);
File topicsDir = new File(docTargetDir, "topics");
if (helpSearchDir.exists()) {
Expand Down Expand Up @@ -397,17 +387,26 @@ public void generateHelpSearch() throws Exception {
}



private File getTargetFile(File sourceFile){
return new File(getTargetDirectory(sourceFile.getParentFile()), sourceFile.getName());
}

private File getTargetDirectory(File sourceDir){
return new File(sourceDir.getPath().replace(docSourceDir.getPath(),docTargetDir.getPath()));
File targetDir = new File(sourceFile.getParentFile().getPath().replace(docSourceDir.getPath(), docTargetDir.getPath()));
return new File(targetDir, sourceFile.getName());
}

private static String getHelpRelativePath(File sourceDir, File targetFile) {
return Paths.get(sourceDir.getPath()).relativize(Paths.get(targetFile.getPath())).toString();
}

@Override
public void writeFiles(Documentation documentation) throws Exception {
writeDefinitions(documentation);
writePages(documentation);

copyTableOfContents();
copyHelpSet();

generateHelpMap(documentation);
generateHelpSearch();

generateWebIndexHtml(documentation);
}
}

0 comments on commit 095a5bf

Please sign in to comment.