summaryrefslogtreecommitdiffhomepage
path: root/dlang/src/ocda/load_pod.d
blob: a99da0fe7467d96a38bafd5414d29dc702a217b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/+
- 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/<uid>.<lang>.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;
}

/+ ↓ <uid>.<lang>.ssp -> <lang> +/
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;
}