summaryrefslogtreecommitdiff
path: root/lib/translate/translate.go
blob: 3c931aa58a4e44ee73c3acb9ac57f1c941cebbc0 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package translate

import (
	"io"
	"pgset/lib/parse"
	"errors"
	"strings"
	"regexp"
)

type State struct {
	// Which type of anchor was last begun: href or id?
	lastAnchor string
	// Consume input until further notice?
	consume bool
	// Stay on the same line of output until further notice?
	sameLine bool
	// Stat on the same line of output at least for this input?
	sameLineOnce bool

	// metadata
	title string
	language string
	creator string
	subject string
}

func chapter(t *parse.Tag, language string) (string, error) {
}

func div(t *parse.Tag, language string, state *State) (string, error) {
	// Divs specify sections of the text, via the "class" attribute
	// class:
	// 	- chapter: new chapter
	// 	- project-gutenberg-license: license text
	//	- secthead: sections of license text
	//	- fig: image
	//	- container: meaningless
	// id:
	//	- pg-start-separator: start of actual text
	// 	- pg-end-separator: end of actual text
	//	- pg-header-authlist: list of authors
	types := []string{"href", "id"}
	// Least bad expression of this idea in this language
	classes := map[string]func (*parse.Tag, string) (string, error) {
		"chapter": chapter,
		"project-gutenberg-license": project-gutenberg-license,
		"secthead": secthead,
		"fig": fig,
		"container": container,
	}
	ids := map[string]func (*parse.Tag, string) (string, error) {
		"pg-start-separator": pg-start-separator,
	 	"pg-end-separator": pg-end-separator,
		"pg-header-authlist": pg-header-authlist,
	}
	if _, ok := t.Attributes["class"]; ok {
	} else if _, ok := t.Attributes["id"]; ok {
	} else {
		// junk
	}
}
func style(state *State) {
	state.consume = true
}
func slashStyle(state *State) {
	state.consume = false
}

func href(t *parse.Tag, language string) (string, error) {
	switch language {
	case "Neatroff":
		// .post.url #chapter23\ntext\n\n
		return ".post.url " + t.Attributes["href"], nil
	default:
		return "", errors.New("Unimplemented")
	}
}

func id(t *parse.Tag, language string) (string, error) {
	switch language {
	case "Neatroff":
		// .post.name chapter23
		return ".post.name " + strings.Replace(t.Attributes["id"], "#", "", 1), nil
	default:
		return "", errors.New("Unimplemented")
	}
}

func a(t *parse.Tag, language string, state *State) (string, error) {
	// There are two kinds of anchors.
	// href: refer to a resource, internal or external
	// id: serve as an anchor to be referred to
	types := []string{"href", "id"}
	// Least bad expression of this idea in this language
	m := map[string]func (*parse.Tag, string) (string, error) {
		"href": href,
		"id": id,
	}
	for _, i := range types {
		if _, ok := t.Attributes[i]; ok {
			state.lastAnchor = i
			s, err := m[i](t, language)
			if err != nil {
				return "", err
			}
			return s, nil
		}
	}
	return "", errors.New("Broken anchor. " + t.String())
}
func slashA(t *parse.Tag, language string, state *State) (string, error) {
	var s string
	switch state.lastAnchor {
	case "href":
		fallthrough
	case "id":
		fallthrough
	default:
		s = "\n"
	}
	state.lastAnchor = ""
	return s, nil
}

func meta(t *parse.Tag, language string, state *State) (string, error) {
	// Multiple types of this tag. Only those with "name" attribute are interesting
	s := ""
	if _, ok := t.Attributes["name"]; ok {
		s += "name"
	}
	return s, nil
}

func i(state *State) (string, error) {
	state.sameLine = true
	return `\fI`, nil
}
func slashI(state *State) (string, error) {
	state.sameLine = false
	state.sameLineOnce = true
	return `\fP`, nil
}
func strong(state *State) (string, error) {
	state.sameLine = true
	return `\fB`, nil
}
func slashStrong(state *State) (string, error) {
	return slashI(state)
}

var FilterMap = map[string]string{
	// HTML Entities be gone
	"\\&[^;]*;": "",
	// hi—there → hi – there
	"([A-Za-z])—([A-Za-z])": "\\1 – \\2",
}
func Filter(in []byte) ([]byte, error) {
	for k, v := range FilterMap {
		regexp, err := regexp.Compile(k)
		if err != nil {
			return []byte{}, err
		}
		in = regexp.ReplaceAll(in, []byte(v))
	}
	return in, nil
}

// Among all languages, these are defined:
//   - Prefix: What should come before a string of body text
//   - Postfix: What should come after a string of body text
//   - Header: Start of document
//   - Footer: End of document
//
// Maps for these tags, beginning and end if non-empty:
//   - a: anchors to bookmarks and urls, switch on attribute
//   - img: images
//   - p: paragraphs
//   - meta, link: metadata
//   - h{1,2,3,4,5}: headings
//   - strong, i: emphasis
//   - table, tbody, tr, td: Table of Contents
//   - div: chapter separations
//   - br: line breaks
//   - blockquote: block quotations
//   - hr: horizontal rule
//
// All other tags are ignored.
//   - span: not meaningful, essentially empty
//   - body: not meaningful
//   - head: not meaningful
//   - Non-standard and atypical extensions
var Definitions = map[string]map[string]any{
	"ConTeXt": {
		// Must generate.
		"a":	a,
		"/a":    ``,
		// Standardized metadata.
		// charset: character set
		//
		"meta":  func(t *parse.Tag) (string, error) {
			var s string
			for k, v := range t.Attributes {
				s = k + v
			}
			return s, nil
		},
		"/meta": ``,
		"div":   func(t *parse.Tag) (string, error) {
			var s string
			for k, v := range t.Attributes {
				s = k + v
			}
			return s, nil
		},
		"/div":  ``,
		"Header": func(t *parse.Tag) (string, error) {
			var s string
			for k, v := range t.Attributes {
				s = k + v
			}
			return s, nil
		},
		"Footer": func(t *parse.Tag) (string, error) {
			var s string
			for k, v := range t.Attributes {
				s = k + v
			}
			return s, nil
		},
		// link tags are empty
		"link": func(t *parse.Tag) (string, error) {
			var s string
			for k, v := range t.Attributes {
				s = k + v
			}
			return s, nil
		},
		// img tags are empty
		"img":    func(t *parse.Tag) (string, error) {
			var s string
			for k, v := range t.Attributes {
				s = k + v
			}
			return s, nil
		},

		// Constants.
		"p":       ``,
		"/p":      ``,
		"i":       ``,
		"/i":      ``,
		"strong":  ``,
		"/strong": ``,
		"table":   ``,
		"/table":  ``,
		"tr":      ``,
		"/tr":     ``,
		"tbody":   ``,
		"/tbody":  ``,
		"td":      ``,
		"/td":     ``,
		"h1":      ``,
		"/h1":     ``,
		"h2":      ``,
		"/h2":     ``,
		"h3":      ``,
		"/h3":     ``,
		"h4":      ``,
		"/h4":     ``,
		"h5":      ``,
		"/h5":     ``,
		"blockquote":	``,
		"/blockquote":	``,
		// br tags are empty
		"br":     ``,
		// hr tags are empty
		"hr":	``,
		"Prefix": ``,
		"Postfix": ``,
	},
	"kerTeX":       {},
	"XeTeX":        {},
	"LaTeX memoir": {},
	"Neatroff": {
		"Header": `.ad pb
.pmll 20 999999
.ss 12 0
.ssh 15
.kn 1
.hlm 2
.ds margin 1.25i
.po \*[margin]
.de header
'	sp \*[margin]
..
.de footer
'	bp
..
.de pg
.	ti +1m
..
.de empty
..
.blm empty
.wh 0 header
.wh -\*[margin] footer
.ds measure 6i
.ll \*[measure]
.ds body_size 11
.ds body_spacing 14
.ps \*[body_size]
.vs \*[body_spacing]
.de reset
.	ps \*[body_size]
.	vs \*[body_spacing]
..
.de p
.	ti +1m
..
.de /p
..
.de i
..
.de /i
..
.de div
.	sp
..
.de /div
.	sp
..
.\" As the file is traversed, record .sy to a temporary file.
.\" Format using sed, source using .so. Idea taken from utmac.
.\" Then, at the document's end, write the TOC from the recording.
.de a
..
.de /a
..
.de strong
.	ft B
..
.de /strong
.	ft
..
.de span
..
.de /span
..
.de h1
.	ft \*[FONT_DISPLAY]
.	ps 60
.	vs 80
..
.de /h1
.	reset
..
.de h2
.	ft \*[FONT_SUBHEAD]
.	ps 40
.	vs 60
..
.de /h2
.	reset
..
.de h3
.	h2
.	ps 30
.	vs 20
..
.de /h3
.	/h2
..
.ds page_width 8.5i
.ds page_height 11i
.ds top_margin 1i
.ds side_margin 2i
.ds bottom_margin \*[side_margin]
.\"
.po \*[side_margin]
.ll \*[page_width]-(\*[side_margin]*2u)
.de head
'	sp \*[top_margin]
..
.wh 0 head
.de foot
'	bp
..
.wh -\*[bottom_margin] foot`,
		"Footer": "\n.bp\n",
		"Prefix": "",
		"Postfix": "",
		"NAE": ``,
		"a": a,
		"/a": slashA,
		// Constants.
		"p":       `.pg`,
		"/p":      ``,
		"i":       i,
		"/i":      slashI,
		"strong":  strong,
		"/strong": slashStrong,
		"table":   ``,
		"/table":  ``,
		"tr":      ``,
		"/tr":     ``,
		"tbody":   ``,
		"/tbody":  ``,
		"td":      ``,
		"/td":     ``,
		"h1":      ``,
		"/h1":     ``,
		"h2":      ``,
		"/h2":     ``,
		"h3":      ``,
		"/h3":     ``,
		"h4":      ``,
		"/h4":     ``,
		"h5":      ``,
		"/h5":     ``,
		"blockquote":	``,
		"/blockquote":	``,
		// br tags are empty
		"br":     ``,
		// hr tags are empty
		"hr":	``,
		"head": ``,
		"/head": ``,
		// Unnecessary copy of title
		"title": style,
		"/title": slashStyle,
		"style": style,
		"/style": slashStyle,
		"html": ``,
		"/html": ``,
		"link": ``,
		"meta": ``,
		"body": ``,
		"/body": ``,
		"div": ``,
		"/div": ``,
		"section": ``,
		"/section": ``,
		"span": ``,
		"/span": ``,
		"li": ``,
		"/li": ``,
		"ul": ``,
		"/ul": ``,
		"img": ``,
	},
	"Utmac":          {},
	"groff":          {},
	"Heirloom Troff": {},
	"SILE":           {},
	"Typst":          {},
}

// These keys defined:
//   - "paper size"
//   - "type family"
//   - "columns"
//   - "justification"
//   - "margins"
//   - "headers"
//   - "page numbers"
var Styles = map[string]map[string]map[string]any{
	"Margins": {
		"Neatroff": {
			"Wide": ``,
			"Thin": ``,
		},
		"ConTeXt": {},
	},
}

var debug bool = false
func Translate(document []any, language string, output io.Writer) error {
	output.Write([]byte(Definitions[language]["Header"].(string) + "\n"))
	state := new(State)
	var err error

	for _, i := range document {
		var out string
		switch t := i.(type) {
		case string:
			if debug {
				output.Write([]byte("|" + t  + "|"+ "\n"))
			}
			out = Definitions[language]["Prefix"].(string) + t + Definitions[language]["Postfix"].(string)
		case *parse.Tag:
			if debug {
				output.Write([]byte(t.String() + "\n"))
			}
			switch u := Definitions[language][t.Name].(type) {
			case string:
				out = u
			case func(*parse.Tag) (string, error):
				out, err = u(t)
			case func(*parse.Tag, string, *State) (string, error):
				out, err = u(t, language, state)
			case func(*State) (string, error):
				out, err = u(state)
			case func(*State):
				u(state)
				out = ""
			case nil:
				out = "dne" + t.String() + "\n"
			}
		}
		if err != nil {
			return err
		}
		if out != "" && out != "\n" && !state.consume {
			if !state.sameLine && !state.sameLineOnce {
				out += "\n"
			}
			if state.sameLineOnce {
				state.sameLineOnce = false
			}
			output.Write([]byte(out))
		} else {
			if debug {
				output.Write([]byte("empty" + "\n"))
			}
		}
	}
	output.Write([]byte(Definitions[language]["Footer"].(string) + "\n"))
	return nil
}