/+ - Name: SisuDoc Spine, Doc Reform [a part of] - Description: documents, structuring, processing, publishing, search - static content generator - Author: Ralph Amissah [ralph.amissah@gmail.com] - Copyright: (C) 2015 (continuously updated, current 2026) Ralph Amissah, All Rights Reserved. - License: AGPL 3 or later: Spine (SiSU), a framework for document structuring, publishing and search Copyright (C) Ralph Amissah This program is free software: you can redistribute it and/or modify it under the terms of the GNU AFERO General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see [https://www.gnu.org/licenses/]. If you have Internet connection, the latest version of the AGPL should be available at these locations: [https://www.fsf.org/licensing/licenses/agpl.html] [https://www.gnu.org/licenses/agpl.html] - Spine (by Doc Reform, related to SiSU) uses standard: - docReform markup syntax - standard SiSU markup syntax with modified headers and minor modifications - docReform object numbering - standard SiSU object citation numbering & system - Homepages: [https://www.sisudoc.org] [https://www.doc-reform.org] - Git [https://git.sisudoc.org/] +/ module ocda.load_pod; /+ ↓ the abstraction out of a pod archive. . A .sisupod is one zstd frame wrapping a stored zip; a .zip is the same archive unwrapped, which is what spine published before it wrapped them. Either way, a pod built with --pod2 carries the abstraction inside it, at media/abstraction/..ssp, and that is what is read. . So this is a third input form and *not* a third model: the .ssp inside is parsed by ocda.load_ssp and comes back as the same value. A writer over the library gains .sisupod without being touched, which is the whole reason the dispatch is in one place. . A pod built without --pod2 carries markup and no abstraction. That is refused, and the refusal says which flag makes a pod this reader can read, because "no abstraction in it" is a fact about the pod rather than about the reader. +/ import std.algorithm : endsWith, sort; import std.array : array; import std.file : read; import std.string : indexOf; import ocda.model; import ocda.load_ssp; import ocda.zip; import ocda.zstd; /+ ↓ a limit on what a frame may expand to. A reader of an artefact it did not make declines to be told how much memory to allocate. 512 MiB is far above the largest pod in the reference collection (4 MiB) and far below anything that matters. +/ enum size_t max_pod_size = 512 * 1024 * 1024; class OcdaPodError : Exception { this(string msg) { super(msg); } } /+ ↓ which languages of the abstraction the pod carries, in sorted order +/ string[] podLanguages(string path) { auto bytes = archiveBytes(path); string[] out_; foreach (e; zipFindSuffix(zipEntries(bytes), ".ssp")) { string lang = langOfSspName(e.name); if (lang.length > 0) { out_ ~= lang; } } return out_.sort.array; } /+ ↓ load one language of the abstraction a pod carries. lang == "" takes the only one there is, and where there is a choice it says so rather than choosing, as the .ocda.db loader does. +/ OcdaDocument loadPodFile(string path, string lang = "") { auto bytes = archiveBytes(path); auto entries = zipEntries(bytes); auto ssps = zipFindSuffix(entries, ".ssp"); if (ssps.length == 0) { throw new OcdaPodError(path ~ ": carries no .ssp." ~ " A pod carries the abstraction only when spine was asked for it:" ~ " spine --pod2 puts it in, spine --source does not."); } ZipEntry chosen = ssps[0]; if (lang.length > 0) { bool found = false; foreach (e; ssps) { if (langOfSspName(e.name) == lang) { chosen = e; found = true; break; } } if (!found) { string have; foreach (i, e; ssps) { have ~= (i > 0 ? " " : "") ~ langOfSspName(e.name); } throw new OcdaPodError(path ~ ": no language '" ~ lang ~ "'; it holds: " ~ have); } } else if (ssps.length > 1) { string have; foreach (i, e; ssps) { have ~= (i > 0 ? " " : "") ~ langOfSspName(e.name); } throw new OcdaPodError(path ~ ": holds more than one language (" ~ have ~ "); name the one wanted"); } auto ssp = zipRead(bytes, chosen); return parseSsp(cast(string) ssp.idup); } /+ ↓ the archive, unwrapped if it was wrapped. The bytes decide, not the name: a .sisupod is a zstd frame and a .zip is not, and a file misnamed either way still reads. +/ package ubyte[] archiveBytes(string path) { auto bytes = cast(ubyte[]) read(path); return zstdIsFrame(bytes) ? zstdDecompress(bytes, max_pod_size) : bytes; } /+ ↓ ..ssp -> +/ package string langOfSspName(string name) { if (!name.endsWith(".ssp")) { return ""; } string stem = name[0 .. $ - 4]; auto dot = stem.lastIndexOf('.'); return (dot > 0) ? stem[dot + 1 .. $] : ""; } private ptrdiff_t lastIndexOf(string s, char c) { for (ptrdiff_t i = s.length - 1; i >= 0; i--) { if (s[i] == c) { return i; } } return -1; }