aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/sisudoc/ocda/meta/topic_register.d
blob: 0c52e6341197c94b413013bd057d730719c1f6c1 (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
/+
- Name: SisuDoc Spine
  - Description: part of SiSU spine (Doc Reform):
    for document structuring, processing, publishing and search

  - Author: Ralph Amissah
    [ralph.amissah@gmail.com]

  - Copyright: (C) 2015 (continuously updated, current 2026) Ralph Amissah,
    All Rights Reserved.

  - License: AGPL 3 or later, for everything in this repository unless a file or
    a directory specifies otherwise.

    This program is free software: you can redistribute it and/or modify it
    under the terms of the GNU Affero General Public License (AGPLv3) as
    published by the Free Software Foundation (FSF), 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 Affero General Public License
    for more details.

    You should have received a copy of the GNU Affero 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: [https://www.gnu.org/licenses/agpl-3.0.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 sisudoc.ocda.meta.topic_register;
@safe:
/+ ↓ the topic register, as the two arrays the curate writers walk
     .
     classify.topic_register is authored as one string, and the topic index
     needs it as a list of paths and as a list of readable labels. The rule
     is not a plain split: a main term is separated from the rest, sub terms
     divided by "|" become one entry each, and the parts are joined with the
     internal separator that the writers then split on again.
     .
     It lives here because two callers need the same answer from the same
     string: the yaml reader, which has just read it from a document header,
     and a document loaded from a .ssp or a .ocda.db, whose header carries
     the string but not the arrays. Deriving it twice is how two answers to
     one question get into a program.
+/
template spineTopicRegister() {
  import std.algorithm : sort, uniq;
  import std.array;
  import std.regex;
  import std.string;
  import sisudoc.ocda.meta.rgx;
  import sisudoc.ocda.meta.defaults;
  mixin spineRgxIn;
  mixin InternalMarkup;
  struct ST_TopicRegister {
    string[] arr;           // topic paths, joined with the internal separator
    string[] expanded;      // the same, as readable ", " separated labels
  }
  ST_TopicRegister topicRegisterArrays(string _topic_register) {
    static auto rgx  = RgxI();
    auto mkup        = InlineMarkup();
    ST_TopicRegister _out;
    if (_topic_register.length == 0) { return _out; }
    string[] _main_topics = _topic_register.strip.split(rgx.topic_register_main_terms_split);
    string[] _topics;
    string   _topics_tmp;
    string[] _multiple_sub_terms;
    foreach (_mt; _main_topics) {
      _topics_tmp = _mt.replaceAll(rgx.topic_register_main_term_plus_rest_split, mkup.sep);
      if (auto m = _topics_tmp.match(rgx.topic_register_multiple_sub_terms_split)) {
        _multiple_sub_terms = m.captures[1].split(rgx.topic_register_sub_terms_split);
        foreach (_subterm; _multiple_sub_terms) {
          _topics ~= m.captures.pre ~ mkup.sep ~ _subterm;
        }
      } else {
        _topics ~= _topics_tmp;
      }
    }
    _out.arr = _topics;
    foreach (_topic; _out.arr) {
      if (_topic.length > 0) { _out.expanded ~= _topic.split(mkup.sep).join(", "); }
    }
    return _out;
  }
}