Skip to content

Commit

Permalink
Merge pull request #82 from metanorma/update_scripts
Browse files Browse the repository at this point in the history
Update scripts
  • Loading branch information
Intelligent2013 authored Oct 15, 2023
2 parents ade6b51 + 9521eb3 commit d2df3f4
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 13 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,10 @@
<artifactId>scala-junction_2.9.1</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
</project>
103 changes: 103 additions & 0 deletions src/main/java/org/metanorma/MetanormaCollectionManifest.java
Original file line number Diff line number Diff line change
@@ -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<Map.Entry<String,String>> inputOutputFiles;

Map<String, Object> yamlObj = new LinkedHashMap<>();

public MetanormaCollectionManifest(List<Map.Entry<String,String>> 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<String, String> 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<String, Object> 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<String, Object> yamlDocumentObj, int num, String documentFolder) {
ArrayList docref =
((ArrayList<Object>)
((LinkedHashMap <String, Object>)
((ArrayList<Object>)
((LinkedHashMap <String, Object>)
yamlDocumentObj.get("manifest"))
.get("manifest"))
.get(num))
.get("docref"));
for (int i = 0; i < docref.size(); i++) {
Map <String, Object> items = (LinkedHashMap <String, Object>)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<Object>)
((LinkedHashMap <String, Object>)
((ArrayList<Object>)
((LinkedHashMap <String, Object>)
yamlObj.get("manifest"))
.get("manifest"))
.get(num))
.get("docref"));
if (template_docref == null) {
template_docref = new ArrayList<>();
}
template_docref.add(items);
((LinkedHashMap <String, Object>)
((ArrayList<Object>)
((LinkedHashMap <String, Object>)
yamlObj.get("manifest"))
.get("manifest"))
.get(num))
.put("docref",template_docref);
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/org/metanorma/MetanormaCover.java
Original file line number Diff line number Diff line change
@@ -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<Map.Entry<String,String>> inputOutputFiles;

public MetanormaCover(List<Map.Entry<String,String>> 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("<html><head/><body>\n" +
" <h1>{{ doctitle }}</h1>\n" +
" <h2>{{ docnumber }}</h2>\n" +
" <nav>{{ nav_object['children'][0]['docrefs'] }}</nav>\n" +
" </body></html>");

BufferedWriter writer = new BufferedWriter(new FileWriter(Paths.get(repositoryRootFolder, "cover.html").toFile()));
writer.write(sbCover.toString());
writer.close();
}
}
62 changes: 62 additions & 0 deletions src/main/java/org/metanorma/ScriptCollection.java
Original file line number Diff line number Diff line change
@@ -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<Map.Entry<String,String>> inputOutputFiles;

public ScriptCollection(List<Map.Entry<String,String>> 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<String> repos = new ArrayList<>();
for (Map.Entry<String,String> 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();
}
}
}
32 changes: 32 additions & 0 deletions src/main/java/org/metanorma/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

}
}
33 changes: 22 additions & 11 deletions src/main/java/org/metanorma/stepmod2mn.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");

Expand Down
28 changes: 28 additions & 0 deletions src/main/resources/collection_template.yml
Original file line number Diff line number Diff line change
@@ -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:

5 changes: 3 additions & 2 deletions src/main/resources/stepmod2mn.resource.adoc.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,10 @@

<xsl:call-template name="generateHtmlAttachmentsSH"/>

<xsl:call-template name="generateCollectionSH">
<!-- commented: https://github.com/metanorma/annotated-express/issues/134 -->
<!-- <xsl:call-template name="generateCollectionSH">
<xsl:with-param name="partnumber" select="resource/@part"/>
</xsl:call-template>
</xsl:call-template> -->

<xsl:variable name="adoc">

Expand Down

0 comments on commit d2df3f4

Please sign in to comment.