Skip to content

Commit

Permalink
* redo changes of MIHO/Post_SpecConf_AsciiDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
festo-i40 committed Sep 2, 2024
1 parent 877397b commit 6fcdcaa
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 8 deletions.
41 changes: 39 additions & 2 deletions src/AasxCsharpLibrary/AdminShellUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,11 +1090,12 @@ public static string GetTemporaryDirectory()

// see: https://stackoverflow.com/questions/6386113/using-system-io-packaging-to-generate-a-zip-file
public static void AddFileToZip(
string zipFilename, string fileToAdd,
string zipFilename,
string fileToAdd,
CompressionOption compression = CompressionOption.Normal,
FileMode fileMode = FileMode.OpenOrCreate)
{
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
using (Package zip = System.IO.Packaging.Package.Open(zipFilename, fileMode))
{
string destFilename = ".\\" + Path.GetFileName(fileToAdd);
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
Expand All @@ -1113,6 +1114,42 @@ public static void AddFileToZip(
}
}

public static void RecursiveAddDirToZip(
Package zip,
string localPath,
string zipPath = "",
CompressionOption compression = CompressionOption.Normal)
{
// enumerate only on this level
foreach (var infn in Directory.EnumerateDirectories(localPath, "*"))
{
// recurse
RecursiveAddDirToZip(
zip,
localPath: infn,
zipPath: Path.Combine(zipPath, Path.GetFileName(infn)),
compression: compression);
}

foreach (var infn in Directory.EnumerateFiles(localPath, "*"))
{
string destFilename = ".\\" + Path.Combine(zipPath, Path.GetFileName(infn));
Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
if (zip.PartExists(uri))
{
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", compression);
using (FileStream fileStream = new FileStream(infn, FileMode.Open, FileAccess.Read))
{
using (Stream dest = part.GetStream())
{
fileStream.CopyTo(dest);
}
}
}
}

//
// some URL enabled path handling
//
Expand Down
19 changes: 16 additions & 3 deletions src/AasxPluginExportTable/Smt/AnyUiDialogueSmtExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static async Task ExportSmtDialogBased(
var panel = new AnyUiStackPanel();
var helper = new AnyUiSmallWidgetToolkit();
var g = helper.AddSmallGrid(5, 2, new[] { "220:", "*" },
var g = helper.AddSmallGrid(6, 2, new[] { "220:", "*" },
padding: new AnyUiThickness(0, 5, 0, 5));
panel.Add(g);
Expand Down Expand Up @@ -153,13 +153,26 @@ public static async Task ExportSmtDialogBased(
colSpan: 2),
(b) => { record.ExportHtml = b; });
// Row 4 : Export PDF
helper.AddSmallLabelTo(g, 4, 0, content: "Export PDF:",
// Row 4 : Export Antora
helper.AddSmallLabelTo(g, 4, 0, content: "Antora style:",
verticalAlignment: AnyUiVerticalAlignment.Center,
verticalContentAlignment: AnyUiVerticalAlignment.Center);
AnyUiUIElement.SetBoolFromControl(
helper.Set(
helper.AddSmallCheckBoxTo(g, 4, 1,
content: "(dedicated sub-folders for images and diagrams)",
isChecked: record.AntoraStyle,
verticalContentAlignment: AnyUiVerticalAlignment.Center),
colSpan: 2),
(b) => { record.AntoraStyle = b; });
// Row 4 : Export PDF
helper.AddSmallLabelTo(g, 5, 0, content: "Export PDF:",
verticalAlignment: AnyUiVerticalAlignment.Center,
verticalContentAlignment: AnyUiVerticalAlignment.Center);
AnyUiUIElement.SetBoolFromControl(
helper.Set(
helper.AddSmallCheckBoxTo(g, 5, 1,
content: "(export command given by options will be executed)",
isChecked: record.ExportPdf,
verticalContentAlignment: AnyUiVerticalAlignment.Center),
Expand Down
47 changes: 44 additions & 3 deletions src/AasxPluginExportTable/Smt/ExportSmt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This source code may use other Open Source software components (see LICENSE.txt)
using AasxPluginExportTable.Table;
using System.Runtime.Intrinsics.X86;
using AnyUi;
using System.IO.Packaging;

namespace AasxPluginExportTable.Smt
{
Expand All @@ -50,6 +51,10 @@ public class ExportSmt
protected StringBuilder _adoc = new StringBuilder();
protected bool _singleFile = true;

protected string _locationPages = "";
protected string _locationImages = "";
protected string _locationDiagrams = "";

protected void ProcessTextBlob(string header, Aas.IBlob blob)
{
// any content
Expand Down Expand Up @@ -171,7 +176,7 @@ protected void ProcessImageLink(Aas.ISubmodelElement sme)
fn = args.fileName;

// save absolute
var absFn = Path.Combine(_tempDir, fn);
var absFn = Path.Combine(_locationImages, fn);
File.WriteAllBytes(absFn, data);
_log?.Info("Image data with {0} bytes writen to {1}.", data.Length, absFn);

Expand Down Expand Up @@ -213,7 +218,7 @@ protected void ProcessUml(Aas.IReferenceElement refel)
if (refel.IdShort.HasContent())
pumlName = AdminShellUtil.FilterFriendlyName(refel.IdShort);
var pumlFn = pumlName + ".puml";
var absPumlFn = Path.Combine(_tempDir, pumlFn);
var absPumlFn = Path.Combine(_locationDiagrams, pumlFn);

// make options
var umlOptions = new ExportUmlRecord();
Expand Down Expand Up @@ -349,6 +354,33 @@ public void ExportSmtToFile(
_tempDir = AdminShellUtil.GetTemporaryDirectory();
log?.Info("ExportSmt: using temp directory {0} ..", _tempDir);

_locationPages = _tempDir;
_locationImages = _tempDir;
_locationDiagrams = _tempDir;

// sub-folders?
if (optionsSmt.AntoraStyle)
{
try
{
_locationPages = Path.Combine(_tempDir, "pages");
_locationImages = Path.Combine(_tempDir, "images");
_locationDiagrams = Path.Combine(Path.Combine(_tempDir, "partials"), "diagrams");

Directory.CreateDirectory(_locationPages);
Directory.CreateDirectory(_locationImages);
Directory.CreateDirectory(Path.Combine(_tempDir, "partials"));
Directory.CreateDirectory(_locationDiagrams);

_log?.Info(StoredPrint.Color.Black,
"Created dedicated sub-folders for pages, images, partials/diagrams.");
}
catch (Exception ex)
{
_log?.Error(ex, "Creating sub-folders within " + _tempDir);
}
}

// predefined semantic ids
var defs = AasxPredefinedConcepts.AsciiDoc.Static;
var mm = MatchMode.Relaxed;
Expand Down Expand Up @@ -402,7 +434,7 @@ public void ExportSmtToFile(
? AdminShellUtil.FilterFriendlyName(_srcSm.IdShort)
: "output";
var adocFn = title + ".adoc";
var absAdocFn = Path.Combine(_tempDir, adocFn);
var absAdocFn = Path.Combine(_locationPages, adocFn);

// write it
File.WriteAllText(absAdocFn, adocText);
Expand Down Expand Up @@ -439,6 +471,7 @@ public void ExportSmtToFile(
else
{
// create zip package
#if __old_
var first = true;
foreach (var infn in Directory.EnumerateFiles(_tempDir, "*"))
{
Expand All @@ -447,6 +480,14 @@ public void ExportSmtToFile(
fileMode: first ? FileMode.Create : FileMode.OpenOrCreate);
first = false;
}
#else
using (Package zip = System.IO.Packaging.Package.Open(fn, FileMode.Create))
{
AdminShellUtil.RecursiveAddDirToZip(
zip,
_tempDir);
}
#endif
log?.Info("ExportSmt: packed all files to {0}", fn);
}

Expand Down
3 changes: 3 additions & 0 deletions src/AasxPluginExportTable/Smt/ExportSmtRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class ExportSmtRecord
[AasxMenuArgument(help: "If true, will include table data in main AsciiDoc file.")]
public bool IncludeTables = true;

[AasxMenuArgument(help: "If true, will do dedicated sub-folders for images and diagrams.")]
public bool AntoraStyle = false;

[AasxMenuArgument(help: "If true, will execute external program to produce HTML from AsciiDic.")]
public bool ExportHtml = false;

Expand Down

0 comments on commit 6fcdcaa

Please sign in to comment.