aboutsummaryrefslogtreecommitdiffhomepage
path: root/misc/util/d/tools/markup_conversion/markup_changes_header_and_content.d
blob: 86792ff2bef873d3fc4146c97a23b29193cdb401 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env rdmd
/+
  - read in file .sst .ssi .ssm
  - loop twice
    - first
      - check for and skip code blocks
      - use unique code marker for endnote markers in text and give an endnote
        number ★1, increment
      - extract all endnotes in array
    - second
      - check that the footnote marker number count matches the number of notes
        in the array
        - if they match either:
            - substitute each endnote marker with the array footnote[number-1]
            - substitute each endnote marker with footnote
              as inlined footnote markup (footnote number not needed)
        - if they do not match exit
  - check whether changes have been made
    - if so write file with inline footnotes in sub-directory converted_output_/
      using the same name as the original file
    - else, exit
+/
import std.stdio;
import std.file;
import std.array : split;
import std.exception;
// import std.range;
import core.stdc.errno;
import std.regex;
import std.format;
import std.conv;
void main(string[] args) {
  static heading_a               = ctRegex!(`^:?[A][~] `, "m");
  static comment                 = ctRegex!(`^%+ `);
  static block_tic_code_open     = ctRegex!("^`{3} code(?:[.](?P<syntax>[a-z][0-9a-z#+_]+))?(?:[(](?P<attrib>[ a-zA-Z0-9;:,]*)[)])?");
  static block_tic_close         = ctRegex!("^(`{3})$","m");
  static block_curly_code_open   = ctRegex!(`^(?:code(?:[.](?P<syntax>[a-z][0-9a-z_]+))?(?:[(](?P<attrib>[ a-zA-Z0-9;:,]*)[)])?[{][ ]*$)`);
  static block_curly_code_close  = ctRegex!(`^([}]code)`);
  auto rgx_endnote_ref           = ctRegex!(`([~]\^)(?P<tail>[)\]]? |$)`, "gm");
  auto rgx_endnote               = ctRegex!(`^\^~\s+(.+|\n)`, "gm");
  char[][] header0Content1(in string src_text) { // cast(char[])
    /+ split string on _first_ match of "^:?A~\s" into [header, content] array/tuple +/
    char[][] header_and_content;
    auto m = (cast(char[]) src_text).matchFirst(heading_a);
    header_and_content ~= m.pre;
    header_and_content ~= m.hit ~ m.post;
    assert(header_and_content.length == 2,
      "document markup is broken, header body split == "
      ~ header_and_content.length.to!string
      ~ "; (header / body array split should == 2 (split is on level A~))"
    );
    return header_and_content;
  }
  foreach(arg; args[1..$]) {
    if (
      !(arg.match(regex(r"--\w+")))
      && arg.match(regex(r"\w+?\.ss[itm]"))
    ) {
      writeln(arg);
      string filename                  = arg;
      try {
        string[] munged_header, munged_contents, munged_endnotes, endnote_refs;
        string text                    = filename.readText;
        char[][] hc                    = header0Content1(text);
        char[] src_header              = hc[0];
        string[] headers               = src_header.to!string.split("\n\n");
        char[] src_txt                 = hc[1];
        string[] paragraphs            = src_txt.to!string.split("\n\n");
        int endnote_ref_count          = 0;
        int[string] type = [
          "curly_code"                 : 0,
          "tic_code"                   : 0,
        ];
        string _tmp_header;
        foreach (h_; headers) {                                                                                          /+ loop to inline endnotes +/
          _tmp_header = "";
          if (h_.match(regex(r"^[@\[]?title[:\]]?"))) {                                                                  // title
            if (auto m = h_.match(regex(r"^@title:(?:\s+(?P<c>.+)|$)"))) {                                               // sisu bespoke markup
              if (m.captures["c"].length == 0) {
                _tmp_header ~= "title:";
              } else {
                _tmp_header ~= "title:\n  main: " ~ "\"" ~ m.captures["c"] ~ "\"";
              }
            } else if (auto m = h_.match(regex(r"^title\s*=\s*(?P<c>.+)"))) {                                            // toml?
              if (m.captures["c"].length == 0) {
                _tmp_header ~= "title:";
              } else {
                _tmp_header ~= "title:\n  main: " ~ "\"" ~ m.captures["c"] ~ "\"";
              }
            } else if (auto m = h_.match(regex(r"^\[title\]"))) {                                                        // toml markup
              _tmp_header ~= "title:";
            } else if (auto m = h_.match(regex(r"^title(?:\s+(?P<c>.+)|\s+\\$)"))) {                                     // sdlang markup
              if (m.captures["c"].length == 0) {
                _tmp_header ~= "title:";
              } else {
                _tmp_header ~= "title:\n  main: " ~ "\"" ~ m.captures["c"] ~ "\"";
              }
            }
            if (h_.match(regex(r"^\s*[:]?(?:main)[:= ]?", "m"))) {
              if (auto m = h_.match(regex(r"^\s+(?P<h>:main):(?:\s+(?P<c>.+)|$)", "m"))) {                                // sisu bespoke markup
                _tmp_header ~= "  main: " ~ "\"" ~ m.captures["c"] ~ "\"";
              } else if (auto m = h_.match(regex(r"^\s*(?P<h>main)\s*=\s*(?P<c>.+)", "m"))) {                            // toml?
                _tmp_header ~= "  main: " ~ m.captures["c"];
              } else if (auto m = h_.match(regex(r"^\s+(?P<h>main)(?:\s*\s*(?P<c>.+)|$)", "m"))) {                       // toml markup
                _tmp_header ~= "  main: " ~ "\"" ~ m.captures["c"] ~ "\"";
              } else if (auto m = h_.match(regex(r"^\s+(?P<h>main)(?:\s+(?P<c>.+)|\s+\\$)", "m"))) {                     // sdlang markup
                _tmp_header ~= "  main: " ~ "\"" ~ m.captures["c"] ~ "\"";
              }
            }
            if (h_.match(regex(r"^\s*[:]?(?:sub(title)?)[:= ]?", "m"))) {
              if (auto m = h_.match(regex(r"^\s+:sub(?:title)?:(?:\s+(?P<c>.+)|$)", "m"))) {                             // sisu bespoke markup
                _tmp_header ~= "  subtitle: " ~ "\"" ~ m.captures["c"] ~ "\"";
              } else if (auto m = h_.match(regex(r"^\s*sub(?:title)?\s*=\s*(?P<c>.+)$", "m"))) {                         // toml?
                _tmp_header ~= "  subtitle: " ~ m.captures["c"];
              } else if (auto m = h_.match(regex(r"^\s+(?:title)?(?:\s*\s*(?P<c>.+)|$)", "m"))) {                        // toml markup
                _tmp_header ~= "  subtitle: " ~ "\"" ~ m.captures["c"] ~ "\"";
              } else if (auto m = h_.match(regex(r"^\s+(?:title)?(?:\s+(?P<c>.+)|\s+\\$)", "m"))) {                      // sdlang markup
                _tmp_header ~= "  subtitle: " ~ "\"" ~ m.captures["c"] ~ "\"";
              }
            }
          }
          if (h_.match(regex(r"^[@\[]?rights[:\]]?"))) {                                                                 // rights
            if (auto m = h_.match(regex(r"^@rights:[ ]+(?P<c>.+)$"))) {                                                  // sisu bespoke markup
              _tmp_header ~= "rights: \n  copyright: \"" ~ m.captures["c"] ~ "\"";
            } else if (auto m = h_.match(regex(r"^@rights:"))) {                                                         // sisu bespoke markup
              _tmp_header ~= "rights:";
            } else if (auto m = h_.match(regex(r"^\[rights\]", "m"))) {                                                  // toml markup
              _tmp_header ~= "rights:";
            } else if (auto m = h_.match(regex(r"^rights:"))) {                                                          // sdlang markup
              _tmp_header ~= "rights:";
            }
            if (h_.match(regex(r"^\s*[:]?copyright[:= ]?", "m"))) {
              if (auto m = h_.match(regex(r"^\s+:copyright:(?:\s+(?P<c>.+)|$)", "m"))) {                                 // sisu bespoke markup
                _tmp_header ~= "  copyright: " ~ "\"" ~ m.captures["c"] ~ "\"";
              } else if (auto m = h_.match(regex(r"^\s*copyright\s*=\s*(?P<c>.+)", "m"))) {                              // toml?
                _tmp_header ~= "  copyright: " ~ m.captures["c"];
              } else if (auto m = h_.match(regex(r"^\s+<h>copyright(?:\s*\s*(?P<c>.+)|$)", "m"))) {                      // toml markup
                _tmp_header ~= "  copyright: " ~ "\"" ~ m.captures["c"] ~ "\"";
              } else if (auto m = h_.match(regex(r"^\s+copyright(?:\s+(?P<c>.+)|\s+\\$)", "m"))) {                       // sdlang markup
                _tmp_header ~= "  copyright: " ~ "\"" ~ m.captures["c"] ~ "\"";
              }
            }
            if (h_.match(regex(r"^\s*[:]?licen[cs]e[:= ]?", "m"))) {
              if (auto m = h_.match(regex(r"^\s+:licen[cs]e:(?:\s+(?P<c>.+)|$)", "m"))) {                                // sisu bespoke markup
                _tmp_header ~= "  license: " ~ "\"" ~ m.captures["c"] ~ "\"";
              } else if (auto m = h_.match(regex(r"^\s*licen[cs]e\s*=\s*(?P<c>.+)$", "m"))) {                            // toml?
                _tmp_header ~= "  license: " ~ m.captures["c"];
              } else if (auto m = h_.match(regex(r"^\s+licen[cs]e(?:\s*\s*(?P<c>.+)|$)", "m"))) {                        // toml markup
                _tmp_header ~= "  license: " ~ "\"" ~ m.captures["c"] ~ "\"";
              } else if (auto m = h_.match(regex(r"^\s+licen[cs]e(?:\s+(?P<c>.+)|\s+\\$)", "m"))) {                      // sdlang markup
                _tmp_header ~= "  license: " ~ "\"" ~ m.captures["c"] ~ "\"";
              }
            }
          }
          if (_tmp_header.length > 0) {
            munged_header ~= _tmp_header;
          } else {
            munged_header ~= h_;
          }
        }
        writeln(munged_header);
        foreach (paragraph; paragraphs) {                                                                                  /+ loop to gather binary endnotes +/
          if ( !( type["curly_code"] == 1 || type["tic_code"] == 1)
            && paragraph.match(rgx_endnote)
          ) {
            munged_endnotes ~= replaceAll!(m => m[1])
              (paragraph, rgx_endnote);
          } else {
            if ( type["curly_code"] == 1 || type["tic_code"] == 1
              || paragraph.matchFirst(block_curly_code_open)
              || paragraph.matchFirst(block_tic_code_open)
            ) { /+ code blocks identified, no munging +/
              if ( type["curly_code"] == 1
                && paragraph.matchFirst(block_curly_code_close)
              ) {
                type["curly_code"] = 0;
              } else if (type["tic_code"] == 1
                && paragraph.matchFirst(block_tic_close)
              ) {
                type["tic_code"] = 0;
              } else if (paragraph.matchFirst(block_curly_code_open)) {
                type["curly_code"] = 1;
              } else if (paragraph.matchFirst(block_tic_code_open)) {
                type["tic_code"] = 1;
              }
              munged_contents ~= paragraph;
            } else { /+ regular content, not a code block +/
              if (auto m = paragraph.matchAll(rgx_endnote_ref)) {
                foreach (n; m) {
                  endnote_ref_count++; // endnote_refs ~= (n.captures[1]);
                }
              }
              paragraph = replaceAll!(m => " \\\\ " )
                (paragraph, regex(r"\s*<(?:/\s*|:)?br>\s*")); // (paragraph, regex(r"(<br>)"));
              munged_contents ~= paragraph;
            }
          }
        }
        {
          import std.outbuffer;
          auto buffer = new OutBuffer();
          foreach (header; munged_header) { /+ loop to inline endnotes +/
            buffer.write(header ~ "\n\n");
          }
          if (munged_endnotes.length == endnote_ref_count) {
            int endnote_count = -1;
            foreach (content; munged_contents) { /+ loop to inline endnotes +/
              content = replaceAll!(m => "~{ " ~ munged_endnotes[++endnote_count] ~ " }~" ~ m["tail"] )
                (content, rgx_endnote_ref); // endnote_ref cannot occur in a code block or else fail
              buffer.write(content ~ "\n\n");
            }
            if (buffer) {
              try {
                string dir_out = "converted_output_";
                string path_and_file_out = dir_out ~ "/" ~ filename;
                dir_out.mkdirRecurse;
                auto f = File(path_and_file_out, "w");
                f.write(buffer);
                // writeln("wrote: ", path_and_file_out);
              } catch (FileException ex) {
                writeln("did not write file");
                // Handle errors
              }
            }
          } else {
            foreach (content; munged_contents) { /+ loop to inline endnotes +/
              buffer.write(content ~ "\n\n");
            }
          }
        }
      } catch (ErrnoException ex) {
        switch(ex.errno) {
          case EPERM:
          case EACCES: // Permission denied
            break;
          case ENOENT: // File does not exist
            break;
          default:     // Handle other errors
            break;
        }
      }
    }
  }
}