Skip to content

Instantly share code, notes, and snippets.

@felpasl
Created June 11, 2019 15:21
Show Gist options
  • Save felpasl/be48e495bca4c65b71f41df4e1dbf0bc to your computer and use it in GitHub Desktop.
Save felpasl/be48e495bca4c65b71f41df4e1dbf0bc to your computer and use it in GitHub Desktop.
#jArchi Create markdown to all Views from file based on smileham/Export to Markdown.ajs
/*
* Export All Views to Markdown
*
* Based on smileham/Export to Markdown.ajs - https://gist.github.com/smileham/578bbbb88dc0ed5a1403f3b98711ec25
* (c) 2018 Steven Mileham
*
*
* Requires jArchi - https://www.archimatetool.com/blog/2018/07/02/jarchi/
*
* Markdown - https://www.markdownguide.org/
*
* Version 1: Include Support to export all Views
* @felpasl
*/
var debug = true;
var embed = false;
var tocMap = [];
var bodyMap = [];
var folder = [];
var i = 0;
function convertToText(type) {
var theString = type.replaceAll("-"," ").split(" ");
var theResult = "";
for (var i=0; i<theString.length; i++){
theResult+= theString[i][0].toUpperCase()+theString[i].substring(1,theString[i].length) + " ";
}
return theResult.trim();
}
function escapeMD(theString){
var newString = theString.replaceAll("<","&lt;").replaceAll("\n>","\n~QUOTE~");
return newString.substring(0,1)+newString.substring(1).replaceAll(">","&gt;").replaceAll("~QUOTE~",">");
}
function generateLink(theString) {
var regex = /[\[\]\#\\\/\"]/gi;
return "#"+theString.toLowerCase().replace(regex,"")
.replaceAll(" ","-")
.replaceAll("\<","lt")
.replaceAll("\>","gt");
}
function CleanFileName(theString) {
var regex = /[\[\]\(\)\#\\\/\"]/gi;
return theString.replace(regex,"")
.replaceAll(" ","-");
}
function toc(d, element){
$(element).children().not("relationship").each(function(e) {
if (e.name) {
headerDepth="";
for (var i=0; i<d; i++){
headerDepth+=" ";
}
var theHash = generateLink(e.name +" ("+ convertToText(e.type)+")");
if (tocMap[theHash]==null) {
tocMap[theHash]=1;
}
else {
tocMap[theHash]+=1;
}
var linkNum="";
if (tocMap[theHash]>1) {
linkNum = "-"+tocMap[theHash];
}
theTOC+="\n"+headerDepth +"* ["+ escapeMD(e.name) +" ("+ convertToText(e.type) +")"+linkNum.replace("-"," ")+"]("+theHash+linkNum+")";
if ($(e).children().not("relationship").length>0) {
d++;
toc(d, e);
d--;
}
}
});
}
function propertiesTable(element) {
var theProperties = element.prop();
var theHeader="";
var theLine="";
var theBody="";
for (var i=0; i<theProperties.length;i++){
theHeader+="|"+theProperties[i];
theLine+="|---";
theBody+="|"+element.prop(theProperties[i]);
}
return "**Properties**\n"+theHeader+"|\n"+theLine+"|\n"+theBody+"|\n";
}
function documentRelationships(element) {
var theHeader = "|From|Relationship|To|Name|Description|"
var theLine = "|---|---|---|---|---|";
var theBody = "";
$(element).rels().each(function(r){
var q= r.concept;
if (r.type!="diagram-model-connection") {
theBody+="|["+ escapeMD(r.source.name) +" ("+ convertToText(r.source.type) +")]("+generateLink(r.source.name +" ("+ convertToText(r.source.type)+")")+")";
theBody+="|"+convertToText(r.type);
if (q.accessType) {
theBody+=" ("+q.accessType+")";
}
if (q.influenceStrength) {
theBody+=" ("+q.influenceStrength+")";
}
theBody+="|["+ escapeMD(r.target.name) +" ("+ convertToText(r.target.type) +")]("+generateLink(r.target.name +" ("+ convertToText(r.target.type)+")")+")";
theBody+="|"+r.name;
theBody+="|"+r.documentation+"|\n";
}
});
return "**Relationships**\n"+theHeader+"\n"+theLine+"\n"+theBody;
}
function nestedDocumentation(d, element, viewName) {
$(element).children().not("relationship").each(function(e) {
if (e.name) {
var headerDepth ="##";
for (var i=0; i<d; i++) {
headerDepth+="#";
}
var theHash = generateLink(e.name +" ("+ convertToText(e.type)+")");
if (bodyMap[theHash]==null) {
bodyMap[theHash]=1;
}
else {
bodyMap[theHash]+=1;
}
var linkNum="";
if (bodyMap[theHash]>1) {
linkNum = " "+bodyMap[theHash];
}
theDocument+="\n"+headerDepth +" "+ escapeMD(e.name) +" ("+ convertToText(e.type) +")"+linkNum+"\n";
e.documentation ? theDocument+="\n"+escapeMD(e.documentation)+"\n":true;
e.prop().length > 0 ? theDocument+="\n"+escapeMD(propertiesTable(e)):true;
$(e).rels().length>0?theDocument+="\n"+escapeMD(documentRelationships(e)):true;
$(e).rels().ends().each(function(r) {
if (r.text) {
theDocument+="\n> "+escapeMD(r.text).replaceAll("\n","\n> ")+"\n";
}
});
$(e).rels().length>0?theDocument += "\n[Up]("+generateLink(viewName)+")\n":true;
debug? console.log(e.name+":"+$(e).children().not("relationship")+":"+e):true;
if ($(e).children().length>0) {
d++;
nestedDocumentation(d, e, viewName);
d--;
}
}
});
}
function generateView(theView, path, filename)
{
if (theView) {
theDocument+="# "+theView.name+"\n";
theTOC = "* [Introduction](#introduction)";
toc(0,theView);
theDocument+="\n"+theTOC+"\n";
theDocument+="\n## Introduction\n";
if (embed==true){
var bytes = $.model.renderViewAsBase64(theView, "PNG", {scale: 2, margin: 10});
theDocument+="\n!["+theView.name+"](data:image/png;base64,"+bytes+")\n";
}
else {theDocument+="\n!["+theView.name+"][embedView]\n";}
theView.documentation!=""?theDocument+="\n"+theView.documentation+"\n":true;
// Notes with no relationships
$(selection).find().not("element").not("relationship").each(function(c){
if (c.text) {
if ($(c).rels().length==0) {
theDocument+="\n"+escapeMD(c.text).replaceAll("\n","\n> ")+"\n";
}
};
})
nestedDocumentation(0, theView, theView.name);
var exportFile = filename;
if(exportFile != null) {
if (!embed) {
var imageName = "img-" + filename;
imageURL = imageName + ".png";
var bytes = $.model.renderViewAsBase64(theView, "PNG", {scale: 2, margin: 10});
$.fs.writeFile(path + imageURL, bytes, "BASE64");
theDocument+="\n[embedView]: "+ imageURL;
}
theDocument+="\nGenerated: "+ new Date().toLocaleString()+"\n";
$.fs.writeFile(path + exportFile + ".md", theDocument);
tocMap = [];
theDocument = "";
bodyMap = [];
theTOC = "";
console.log("> Export done");
}
else {
console.log("> Export cancelled");
}
}
else {
console.log("> Please Select a View");
}
}
function getView(item)
{
if(item.type == "archimate-diagram-model")
{
console.log("FileName:" + baseDir + folder.join('-') + "-" + CleanFileName(item.name) + ".md")
generateView(item, baseDir, (folder.join('-') ? folder.join('-') + "-" : "") + CleanFileName(item.name));
}
}
function getFolder(item)
{
$(item).children().each(function(child){
if(child.type == "folder")
{
folder[i] = child.name;
i++;
getFolder(child);
i--;
folder.pop();
}
if(child.type == "archimate-diagram-model")
{
getView(child);
}
});
}
console.show();
console.clear();
console.log("Export to Markdown");
var theDocument = "";
var theView = $(selection).filter("archimate-diagram-model").first();
var baseDir;;
$(selection).each(function(element) {
if(element.type == "folder" && element.name == "Views")
{
baseDir = window.prompt("Base Directory", "Directory") + "\\";
console.log("baseDir: " + baseDir);
getFolder(element);
}
else
{
console.log("> Please select a View Folder")
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment