interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / apps / interplane-site / native / translate.om

780 lines · 12019 bytes Raw Blame History
1note Script-free reader support. Uses the builder arena/output and its existing checked capacities.
2note No translation, browser code, storage or endpoints. Transform and validate share lexical rules.
3text trrad "rad:"
4text trdid "did:"
5text trzero ""
6text trno " translate=\"no\""
7text trspan "<span translate=\"no\">"
8text trend "</span>"
9text tren "<span lang=\"en\">"
10text trattr "translate"
11text trclass "class"
12text trlang "lang"
13text trcode "code"
14text trpre "pre"
15text trkbd "kbd"
16text trsamp "samp"
17text trbrand "brand"
18text trmono "mono"
19text trdigest "digest"
20text trfiles "files"
21text trtitle "title"
22text trnov "no"
23text trerr "translation protection missing or overridden (hash, ID, file, command or code)"
24text trlayout "translation links need a head, header and footer"
25text trtxt "index.txt"
26text trjson "index.json"
27text trhtml ".html"
28text trtxext ".txt"
29text trjsext ".json"
30text tralt1 "<link rel=\"alternate\" type=\"text/plain\" href=\""
31text tralt2 "<link rel=\"alternate\" type=\"application/json\" href=\""
32text tralt3 "\">\n"
33text trhead "</header>"
34text trhlink "<div class=\"shell\"><a class=\"text-link\" lang=\"en\" href=\"/translate/\">Read in your language</a></div>"
35text trflink "<p class=\"foot-fine\" lang=\"en\"><a href=\"/translate/\">Read in your language</a> | <a href=\""
36text trtextl "\">Plain text</a> | <a href=\""
37text trjsonl "\">JSON source</a></p>\n"
38text trj1 "{\"source_url\":\""
39text trj2 "\",\"lang\":\""
40text trj3 "\",\"text\":\""
41text trj4 "\",\"html\":\""
42text trj5 "\"}\n"
43text trcspnm "content-security-policy:"
44text trscript "script-src"
45text trnone "'none'"
46text trcspbad "served response must have script-src 'none' without other script sources or overrides"
47note trq: attribute value offset/length, depth, protected depth, explicit lang depth, raw depth.
48global trq ulong 6
49
50fn trat a ulong e ulong t view uchar * to bool
51if gt add a len t e
52ret false
53end
54ret mistf a t false
55end
56
57fn trname a ulong e ulong to ulong
58k = a
59while lt k e bound 9223372036854775807
60if not namechar mg k
61break
62end
63k = add k 1
64end
65ret k
66end
67
68fn trtagend a ulong e ulong to ulong
69q = uchar 0
70k = a
71while lt k e bound 9223372036854775807
72c = mg k
73if ne q 0
74if eq c q
75q = 0
76end
77else
78if or eq c 34 eq c 39
79q = c
80else
81if eq c 62
82ret add k 1
83end
84end
85end
86k = add k 1
87end
88die ermark
89ret e
90end
91
92fn trget a ulong e ulong key view uchar * to bool
93note Parse attributes, not substrings inside other attribute values. Markup has already been checked.
94k = a
95put trq 0 0
96put trq 1 0
97while lt k e bound 9223372036854775807
98if space mg k
99k = add k 1
100continue
101end
102s = k
103k = trname k e
104if eq s k
105ret false
106end
107hit = mist s sub k s key
108while lt k e bound 9223372036854775807
109if not space mg k
110break
111end
112k = add k 1
113end
114if eq mg k 61
115k = add k 1
116while space mg k bound 9223372036854775807
117k = add k 1
118end
119q = mg k
120k = add k 1
121v = k
122while lt k e bound 9223372036854775807
123if eq mg k q
124break
125end
126k = add k 1
127end
128if hit
129put trq 0 v
130put trq 1 sub k v
131ret true
132end
133k = add k 1
134end
135end
136ret false
137end
138
139fn trclasses a ulong e ulong to bool
140if not trget a e trclass
141ret false
142end
143k = get trq 0
144lim = add k get trq 1
145while lt k lim bound 9223372036854775807
146if space mg k
147k = add k 1
148continue
149end
150s = k
151while lt k lim bound 9223372036854775807
152if space mg k
153break
154end
155k = add k 1
156end
157n = sub k s
158if or mist s n trbrand mist s n trmono
159ret true
160end
161if or mist s n trdigest mist s n trfiles
162ret true
163end
164end
165ret false
166end
167
168fn trmust a ulong n ulong e ulong to bool
169z = sub n a
170if or mist a z trcode mist a z trpre
171ret true
172end
173if or mist a z trkbd mist a z trsamp
174ret true
175end
176ret trclasses n e
177end
178
179fn trvoid a ulong n ulong e ulong to bool
180if eq mg sub e 2 47
181ret true
182end
183z = sub n a
184if or mist a z vtmeta mist a z vtlink
185ret true
186end
187if or mist a z vtbr mist a z vtimg
188ret true
189end
190if or mist a z vthr mist a z vtwbr
191ret true
192end
193ret or mist a z vtinput or mist a z vtsource mist a z vtcol
194end
195
196fn trwc c uchar to bool
197ret or alnum c or eq c 95 or eq c 45 or eq c 46 or eq c 47 or eq c 64 eq c 58
198end
199
200fn trtoken a ulong e ulong to bool
201note Protect a hex run of 7+ (upper or lower), identifier prefixes, and path/file-like words.
202k = a
203hxrun = ulong 0
204digit = false
205dot = false
206while lt k e bound 9223372036854775807
207c = mg k
208if lt hexv c 16
209hxrun = add hxrun 1
210if and ge c 48 le c 57
211digit = true
212end
213if and ge hxrun 7 digit
214ret true
215end
216else
217hxrun = 0
218digit = false
219end
220if or eq c 46 eq c 47
221dot = true
222end
223k = add k 1
224end
225if dot
226ret true
227end
228if or trat a e trrad trat a e trdid
229ret true
230end
231if ge sub e a 29
232if eq mg a 122
233ret true
234end
235end
236ret false
237end
238
239fn trword s ulong k ulong mode ulong to void
240t = k
241while gt t s bound 9223372036854775807
242c = mg sub t 1
243if not or eq c 46 or eq c 58 or eq c 45 eq c 47
244break
245end
246t = sub t 1
247end
248hit = false
249if gt t s
250hit = trtoken s t
251end
252if and hit eq get trq 3 0
253if eq mode 1
254die trerr
255end
256ot trspan
257om s sub t s
258ot trend
259om t sub k t
260else
261if eq mode 0
262om s sub k s
263end
264end
265end
266
267fn trtext a ulong e ulong mode ulong to void
268k = a
269while lt k e bound 9223372036854775807
270if not trwc mg k
271if eq mode 0
272ob mg k
273end
274k = add k 1
275continue
276end
277s = k
278while lt k e bound 9223372036854775807
279if not trwc mg k
280break
281end
282k = add k 1
283end
284trword s k mode
285end
286end
287
288fn tropen a ulong n ulong e ulong mode ulong to void
289must = and trmust a n e eq get trq 3 0
290has = trget n e trattr
291no = false
292if has
293no = mist get trq 0 get trq 1 trnov
294if and not no or must ne get trq 3 0
295die trerr
296end
297end
298if and must not no
299if eq mode 1
300die trerr
301end
302no = true
303end
304if eq mode 0
305om sub a 1 add sub n a 1
306if and must not has
307ot trno
308end
309om n sub e n
310end
311if trvoid a n e
312ret
313end
314d = add get trq 2 1
315put trq 2 d
316if and no eq get trq 3 0
317put trq 3 d
318end
319if and eq get trq 4 0 trget n e trlang
320put trq 4 d
321end
322if mist a sub n a trtitle
323put trq 5 d
324end
325end
326
327fn trwalk a ulong e ulong mode ulong zh bool to void
328for j ulong 6
329put trq j 0
330end
331k = a
332while lt k e bound 9223372036854775807
333if eq mg k 60
334if trat k e tcmo
335q = mfirst k e tcme
336if eq q e
337die ermark
338end
339q = add q len tcme
340if eq mode 0
341om k sub q k
342end
343k = q
344continue
345end
346q = trtagend k e
347j = add k 1
348if eq mg j 47
349d = get trq 2
350if eq get trq 3 d
351put trq 3 0
352end
353if eq get trq 4 d
354put trq 4 0
355end
356if eq get trq 5 d
357put trq 5 0
358end
359if gt d 0
360put trq 2 sub d 1
361end
362if eq mode 0
363om k sub q k
364end
365else
366n = trname j q
367if eq n j
368if eq mode 0
369om k sub q k
370end
371else
372tropen j n q mode
373end
374end
375k = q
376else
377s = k
378while lt k e bound 9223372036854775807
379if eq mg k 60
380break
381end
382k = add k 1
383end
384if ne get trq 5 0
385if eq mode 0
386om s sub k s
387end
388else
389trtext s k mode
390end
391end
392end
393end
394
395fn trcheck to void
396note Validation is run on reread output too; a changed manifest does not bypass it.
397mark = sg TOP
398n = sg OBN
399a = stash
400trwalk a add a n 1 false
401put st TOP mark
402put st OBN n
403end
404
405fn trurl r ulong suffix view uchar * absolute bool to void
406b = mul r 8
407if absolute
408zurl r
409else
410om rg add b 2 rg add b 3
411end
412ot suffix
413end
414
415fn trlinks r ulong to void
416a = stash
417e = add a sg FLEN
418h = mfirst a e zhead
419v = mfirst a e trhead
420f = mlast a e zfoot
421if or eq h e or eq v e eq f e
422die trlayout
423end
424om a sub h a
425ot tralt1
426trurl r trtxt false
427ot tralt3
428ot tralt2
429trurl r trjson false
430ot tralt3
431om h sub v h
432ot trhlink
433om v sub f v
434ot trflink
435trurl r trtxt false
436ot trtextl
437trurl r trjson false
438ot trjsonl
439om f sub e f
440end
441
442fn trpage r ulong to void
443trlinks r
444note Run the existing text policy and markup checks first so refused input keeps its diagnostic.
445polscan true
446chkmark true
447a = stash
448e = add a sg FLEN
449trwalk a e 0 false
450end
451
452fn trjesc a ulong n ulong to void
453for k ulong n bound 9223372036854775807
454c = mg add a k
455if or eq c 34 eq c 92
456ob 92
457ob c
458else
459if lt c 32
460ob 92
461ob 117
462ob 48
463ob 48
464ob hexc shr ulong c 4
465ob hexc and ulong c 15
466else
467ob c
468end
469end
470end
471end
472
473text trmaino "<main"
474text trmaine "</main>"
475text trbr "br"
476text trp "p"
477text trli "li"
478text trtd "td"
479text trdiv "div"
480text trsect "section"
481text trrow "tr"
482text trh1 "h1"
483text trh2 "h2"
484text trh3 "h3"
485text trsvg "svg"
486text trapos "&apos;"
487text trnbsp "&nbsp;"
488text trarr "&rarr;"
489text trldq "&ldquo;"
490text trrdq "&rdquo;"
491text tromega "&Omega;"
492text trmidd "&middot;"
493text trback "```\n"
494
495fn trnl to void
496if gt sg OBN 0
497if ne og sub sg OBN 1 10
498ob 10
499end
500end
501end
502
503fn trentity k ulong e ulong to ulong
504if trat k e hamp
505ob 38
506ret add k len hamp
507end
508if trat k e hlt
509ob 60
510ret add k len hlt
511end
512if trat k e hgt
513ob 62
514ret add k len hgt
515end
516if trat k e hquot
517ob 34
518ret add k len hquot
519end
520if trat k e trapos
521ob 39
522ret add k len trapos
523end
524if trat k e ptrade
525ot ttm
526ret add k len ptrade
527end
528if trat k e trnbsp
529ob 32
530ret add k len trnbsp
531end
532if trat k e trarr
533ob 45
534ob 62
535ret add k len trarr
536end
537if or trat k e trldq trat k e trrdq
538ob 34
539ret add k len trldq
540end
541if trat k e tromega
542ob 206
543ob 169
544ret add k len tromega
545end
546if trat k e trmidd
547ob 194
548ob 183
549ret add k len trmidd
550end
551ob mg k
552ret add k 1
553end
554
555fn trblock a ulong n ulong to bool
556z = sub n a
557if or mist a z trp mist a z trli
558ret true
559end
560if or mist a z trdiv mist a z trsect
561ret true
562end
563if or mist a z trrow mist a z trbr
564ret true
565end
566if or mist a z trh1 mist a z trh2
567ret true
568end
569ret mist a z trh3
570end
571
572fn trplain a ulong e ulong to void
573note Text from the same complete HTML; tags do not turn inline words into separate words, but an
574note element that starts right after a sibling closed is set apart, as a reader sees adjacent links.
575n = mfirst a e trmaino
576if eq n e
577die erhtml
578end
579k = trtagend n e
580lim = mfirst k e trmaine
581if eq lim e
582die erhtml
583end
584pre = false
585skip = false
586sep = false
587while lt k lim bound 9223372036854775807
588if eq mg k 60
589q = trtagend k lim
590j = add k 1
591cl = eq mg j 47
592if cl
593j = add j 1
594end
595n = trname j q
596if mist j sub n j trsvg
597skip = not cl
598end
599if mist j sub n j trpre
600trnl
601ot trback
602pre = not cl
603end
604if and not skip trblock j n
605trnl
606sep = false
607else
608if and and sep not cl not skip
609if gt sg OBN 0
610if not space og sub sg OBN 1
611ob 32
612end
613end
614sep = false
615end
616if and cl not skip
617sep = true
618end
619end
620if mist j sub n j trtd
621ob 9
622end
623k = q
624else
625if skip
626k = add k 1
627else
628if eq mg k 38
629sep = false
630k = trentity k lim
631else
632c = mg k
633if and not pre space c
634if gt sg OBN 0
635if not space og sub sg OBN 1
636ob 32
637end
638end
639else
640sep = false
641ob c
642end
643k = add k 1
644end
645end
646end
647end
648trnl
649end
650
651fn trwrite r ulong suffix view uchar * to void
652b = mul r 8
653pbroot rg add b 1
654pbaddm rg add b 2 rg add b 3
655pbadd suffix
656wrfile rg add b 1
657end
658
659fn trforms r ulong to void
660note HTML already passed policy checks; derivative bytes are checked by exact manifest replay.
661a = stash
662n = sg FLEN
663trplain a add a n
664trwrite r trtxt
665p = stash
666pn = sg FLEN
667ot trj1
668trurl r trzero true
669ot trj2
670ot zen
671ot trj3
672trjesc p pn
673ot trj4
674trjesc a n
675ot trj5
676trwrite r trjson
677end
678
679fn trformpath s ulong e ulong to bool
680if ge sub e s len trtxt
681if mist sub e len trtxt len trtxt trtxt
682ret true
683end
684end
685if ge sub e s len trjson
686if mist sub e len trjson len trjson trjson
687ret true
688end
689end
690ret false
691end
692
693fn trcli to void
694n = argread 2 pb 0 len pb
695if le n 0
696die erarg
697end
698put st PBL ulong n
699put st TOP 0
700a = slurp
701sz = sg FLEN
702put st OBN 0
703om a sz
704put st CXO a
705put st CXL 0
706chkmark true
707trcheck
708end
709
710fn trheaders to void
711n = argread 2 pb 0 len pb
712if le n 0
713die erarg
714end
715put st PBL ulong n
716put st TOP 0
717a = slurp
718lim = add a sg FLEN
719found = false
720k = a
721while lt k lim bound 9223372036854775807
722e = eol k lim
723if trat k e trcspnm
724p = add k len trcspnm
725while lt p e bound 9223372036854775807
726while lt p e bound 9223372036854775807
727if not or space mg p eq mg p 59
728break
729end
730p = add p 1
731end
732s = p
733while lt p e bound 9223372036854775807
734if or space mg p eq mg p 59
735break
736end
737p = add p 1
738end
739is = mist s sub p s trscript
740if and trat s p trscript not is
741die trcspbad
742end
743if is
744while lt p e bound 9223372036854775807
745if not space mg p
746break
747end
748p = add p 1
749end
750if not trat p e trnone
751die trcspbad
752end
753p = add p len trnone
754while lt p e bound 9223372036854775807
755if not space mg p
756break
757end
758p = add p 1
759end
760if lt p e
761if ne mg p 59
762die trcspbad
763end
764end
765found = true
766end
767while lt p e bound 9223372034775807
768if eq mg p 59
769break
770end
771p = add p 1
772end
773end
774end
775k = add e 1
776end
777if not found
778die trcspbad
779end
780end