diff --git a/pom.xml b/pom.xml index 1b68491..1efec02 100644 --- a/pom.xml +++ b/pom.xml @@ -120,5 +120,10 @@ scala-junction_2.9.1 0.1.0 + + org.yaml + snakeyaml + 2.1 + diff --git a/src/main/java/org/metanorma/MetanormaCollectionManifest.java b/src/main/java/org/metanorma/MetanormaCollectionManifest.java new file mode 100644 index 0000000..a54c7bc --- /dev/null +++ b/src/main/java/org/metanorma/MetanormaCollectionManifest.java @@ -0,0 +1,103 @@ +package org.metanorma; + +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +import java.io.*; +import java.nio.file.Paths; +import java.util.*; + +// To generate the collection manifest +// see https://github.com/metanorma/annotated-express/issues/134 +public class MetanormaCollectionManifest { + + List> inputOutputFiles; + + Map yamlObj = new LinkedHashMap<>(); + + public MetanormaCollectionManifest(List> inputOutputFiles) { + this.inputOutputFiles = inputOutputFiles; + try { + Yaml yaml = new Yaml(); + InputStream inputStream = Util.getStreamFromResources(stepmod2mn.class.getClassLoader(), "collection_template.yml"); + yamlObj = yaml.load(inputStream); + //System.out.println(yamlObj); + } catch (Exception e) { + e.printStackTrace(System.err); + } + } + + public void generate() throws IOException { + // get repository root folder from 1st file + String repositoryRootFolder = Util.getRepositoryRootFolder(inputOutputFiles.get(0).getValue()); + if (!repositoryRootFolder.isEmpty()) { + int counter = 0; + for (Map.Entry entry : inputOutputFiles) { + String resultAdoc = entry.getValue(); + String documentFolder = new File(resultAdoc).getParent(); + File fileResultCollectionYml = Paths.get(documentFolder, "collection.yml").toFile(); + InputStream is = new FileInputStream(fileResultCollectionYml); + Yaml yaml = new Yaml(); + Map yamlDocumentObj = yaml.load(is); + //System.out.println(yamlDocumentObj); + + // manifest: + // - level: document + update_docref(yamlDocumentObj,0, documentFolder); + + // manifest: + // - level: attachments + update_docref(yamlDocumentObj,1, documentFolder); + + counter++; + } + + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + Yaml yaml = new Yaml(options); + PrintWriter writer = new PrintWriter(Paths.get(repositoryRootFolder, "collection.yml").toFile()); + yaml.dump(yamlObj, writer); + } + } + + private void update_docref(Map yamlDocumentObj, int num, String documentFolder) { + ArrayList docref = + ((ArrayList) + ((LinkedHashMap ) + ((ArrayList) + ((LinkedHashMap ) + yamlDocumentObj.get("manifest")) + .get("manifest")) + .get(num)) + .get("docref")); + for (int i = 0; i < docref.size(); i++) { + Map items = (LinkedHashMap )docref.get(i); + String fileref = (String)items.get("fileref"); + + String fullPath = Paths.get(documentFolder, fileref).toFile().getAbsolutePath().replace("\\","/"); + items.put("fileref",fullPath); + + // add updated structure into yaml object + ArrayList template_docref = + ((ArrayList) + ((LinkedHashMap ) + ((ArrayList) + ((LinkedHashMap ) + yamlObj.get("manifest")) + .get("manifest")) + .get(num)) + .get("docref")); + if (template_docref == null) { + template_docref = new ArrayList<>(); + } + template_docref.add(items); + ((LinkedHashMap ) + ((ArrayList) + ((LinkedHashMap ) + yamlObj.get("manifest")) + .get("manifest")) + .get(num)) + .put("docref",template_docref); + } + } +} diff --git a/src/main/java/org/metanorma/MetanormaCover.java b/src/main/java/org/metanorma/MetanormaCover.java new file mode 100644 index 0000000..ac063a6 --- /dev/null +++ b/src/main/java/org/metanorma/MetanormaCover.java @@ -0,0 +1,37 @@ +package org.metanorma; + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.List; +import java.util.Map; + +// To generate the cover page +// see https://github.com/metanorma/annotated-express/issues/134 +public class MetanormaCover { + + + List> inputOutputFiles; + + public MetanormaCover(List> inputOutputFiles) { + this.inputOutputFiles = inputOutputFiles; + } + + public void generate() throws IOException { + // get repository root folder from 1st file + String repositoryRootFolder = Util.getRepositoryRootFolder(inputOutputFiles.get(0).getValue()); + + StringBuilder sbCover = new StringBuilder(); + + sbCover.append("\n" + + "

{{ doctitle }}

\n" + + "

{{ docnumber }}

\n" + + " \n" + + " "); + + BufferedWriter writer = new BufferedWriter(new FileWriter(Paths.get(repositoryRootFolder, "cover.html").toFile())); + writer.write(sbCover.toString()); + writer.close(); + } +} diff --git a/src/main/java/org/metanorma/ScriptCollection.java b/src/main/java/org/metanorma/ScriptCollection.java new file mode 100644 index 0000000..c8fef78 --- /dev/null +++ b/src/main/java/org/metanorma/ScriptCollection.java @@ -0,0 +1,62 @@ +package org.metanorma; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +// Generate collection.sh +// The script to process each part, and generate the overall collection +// see https://github.com/metanorma/annotated-express/issues/134 +public class ScriptCollection { + + List> inputOutputFiles; + + public ScriptCollection(List> inputOutputFiles) { + this.inputOutputFiles = inputOutputFiles; + } + + public void generate() throws IOException { + // get repository root folder from 1st file + String repositoryRootFolder = Util.getRepositoryRootFolder(inputOutputFiles.get(0).getValue()); + if(!repositoryRootFolder.isEmpty()) { + String documentFolder = new File(inputOutputFiles.get(0).getValue()).getParent(); + String documentRelativeFolder = Util.getRelativePath(repositoryRootFolder, documentFolder); + String documentsFolder = new File(documentFolder).getParent(); + String documentsRelativeFolder = Util.getRelativePath(documentsFolder, repositoryRootFolder); + + StringBuilder sbScript = new StringBuilder(); + sbScript.append("INPUT_REPOS=\""); + List repos = new ArrayList<>(); + for (Map.Entry entry: inputOutputFiles) { + File f = new File(entry.getKey()); + repos.add(f.getParentFile().getName()); + } + sbScript.append(repos.toString() + .replace("[", "") + .replace("]", "") + .replace(" ", "") + .replace(","," ")).append("\"\n\n"); + + sbScript.append("for name in $INPUT_REPOS").append("\n") + .append("do").append("\n") + .append(" echo $name").append("\n") + // cd data/resource_docs/$name + .append(" cd ").append(documentsRelativeFolder).append("/$name").append("\n") + .append(" sh ./html_attachments.sh").append("\n") + .append(" bundle exec metanorma -x xml document.adoc").append("\n") + // cd ../../.." + .append(" cd ").append(documentRelativeFolder).append("\n") + .append("done").append("\n") + .append("bundle exec metanorma collection collection.yml -x xml,html,presentation -w iso10303-output -c cover.html").append("\n"); + + BufferedWriter writer = new BufferedWriter(new FileWriter(Paths.get(repositoryRootFolder, "collection.sh").toFile())); + writer.write(sbScript.toString()); + writer.close(); + } + } +} diff --git a/src/main/java/org/metanorma/Util.java b/src/main/java/org/metanorma/Util.java index 6f7f636..51990a5 100644 --- a/src/main/java/org/metanorma/Util.java +++ b/src/main/java/org/metanorma/Util.java @@ -270,4 +270,36 @@ public static boolean fileExists(String filename) { Path path = Paths.get(filename); return Files.exists(path); } + + public static String getRepositoryRootFolder(String startFolder) { + // find repository_index.xml + RepositoryIndex repositoryIndex = new RepositoryIndex(startFolder); + String repositoryIndexPath = repositoryIndex.getPath(); + if (!repositoryIndexPath.isEmpty()) { + return new File(repositoryIndexPath).getParent(); + } else { + // if repository_index.xml isn't found, then find folder that + // contains data/resource_docs + + boolean endFoldersTree = false; + + Path repositoryRootPath = Paths.get(startFolder, "data","resource_docs"); + + while (!Files.exists(repositoryRootPath) && !endFoldersTree) { + try { + startFolder = Paths.get(startFolder).getParent().toString(); + repositoryRootPath = Paths.get(startFolder,"data","resource_docs"); + } catch (Exception ex) { + System.err.println("Can't find the repository root folder."); + endFoldersTree = true; + } + } + if (endFoldersTree) { + return ""; + } else { + return repositoryRootPath.toString(); + } + } + + } } diff --git a/src/main/java/org/metanorma/stepmod2mn.java b/src/main/java/org/metanorma/stepmod2mn.java index 7996d88..4f71191 100644 --- a/src/main/java/org/metanorma/stepmod2mn.java +++ b/src/main/java/org/metanorma/stepmod2mn.java @@ -438,18 +438,29 @@ public static void main(String[] args) throws ParseException { inputOutputFiles.removeAll(badInputOutputFiles); - //if (isInputFolder) { - // Generate metanorma.yml in the root of path - //new MetanormaCollection(inputOutputFiles).generate(inputFolder); - String metanormaCollectionPath = argOutputPath; - if (isStandaloneXML) { - metanormaCollectionPath = new File(metanormaCollectionPath).getParent(); - } - if (metanormaCollectionPath == null || metanormaCollectionPath.isEmpty()) { - metanormaCollectionPath = inputFolder; + if (!inputOutputFiles.isEmpty()) { + // Generate collection.sh + new ScriptCollection(inputOutputFiles).generate(); + + // Generate collection manifest collection.yml + new MetanormaCollectionManifest(inputOutputFiles).generate(); + + // Generate cover.html + new MetanormaCover(inputOutputFiles).generate(); + + //if (isInputFolder) { + // Generate metanorma.yml in the root of path + //new MetanormaCollection(inputOutputFiles).generate(inputFolder); + String metanormaCollectionPath = argOutputPath; + if (isStandaloneXML) { + metanormaCollectionPath = new File(metanormaCollectionPath).getParent(); + } + if (metanormaCollectionPath == null || metanormaCollectionPath.isEmpty()) { + metanormaCollectionPath = inputFolder; + } + new MetanormaCollection(inputOutputFiles).generate(metanormaCollectionPath); + //} } - new MetanormaCollection(inputOutputFiles).generate(metanormaCollectionPath); - //} System.out.println("End!"); diff --git a/src/main/resources/collection_template.yml b/src/main/resources/collection_template.yml new file mode 100644 index 0000000..85ad080 --- /dev/null +++ b/src/main/resources/collection_template.yml @@ -0,0 +1,28 @@ +--- +directives: +- documents-inline +bibdata: + title: + - language: en + content: Industrial automation systems and integration -- Product data representation + and exchange -- Integrated generic resource + docid: + type: iso + id: '10303' + edition: '7' + copyright: + owner: + name: International Standards Organization + abbreviation: ISO + from: '2021' +manifest: + level: collection + title: ISO collection + manifest: + - level: document + title: Document + docref: + - level: attachments + title: Attachments + docref: + diff --git a/src/main/resources/stepmod2mn.resource.adoc.xsl b/src/main/resources/stepmod2mn.resource.adoc.xsl index 63d28fa..e2bee14 100644 --- a/src/main/resources/stepmod2mn.resource.adoc.xsl +++ b/src/main/resources/stepmod2mn.resource.adoc.xsl @@ -281,9 +281,10 @@ - + +