-
Notifications
You must be signed in to change notification settings - Fork 0
/
tkadle.tcl
executable file
·2895 lines (2620 loc) · 96.9 KB
/
tkadle.tcl
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# TCL ignores the next line -*- tcl -*- \
exec wish "$0" -- "$@"
###############################################################################
# tkadle -- Single file source code program (Pronounced Teakaddle) originally
# named as a "AsciiDoc List Editor" for creation and modification
# of AsciiDoc list text files. For ease of deployment, tcllib and
# other external packages are avoided.
################################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
###############################################################################
# Presence of TCL OO classes necessitates at least version 8.6
if {[catch {package require Tcl 8.6}]} {
puts stderr "tkadle requires Tcl 8.6 or higher."
puts stderr "Detected Tcl version: [info tclversion]"
exit 1
}
set commandError 0
set commandFile ""
set paradigm "LINELIST"
for {set argIndex 0} {$argIndex < $argc} {incr argIndex} {
switch -exact [lindex $argv $argIndex] {
"-syn" {
incr argIndex
if {$paradigm eq "LINELIST"} {
set paradigm [string toupper [lindex $argv $argIndex]]
} else {
set commandError 1
}
}
default {
if {$commandFile eq ""} {
set commandFile [lindex $argv $argIndex]
} else {
set commandError 1
}
}
}
}
# Handle syntax option via file extension
switch -exact -- $paradigm {
"ADOC" {set paradigm "ASCIIDOC"}
"MD" {set paradigm "MARKDOWN"}
}
if {$paradigm ni [list "ASCIIDOC" "LINELIST" "MARKDOWN"]} {
puts stderr "Invalid syntax option detected"
set commandError 1
}
if {$commandError || ($commandFile eq "")} {
puts stderr "Usage: tkadle.tcl \[-syn asciidoc|adoc|markdown|md\] filename"
exit
}
#----------------------------------------------------------------------------
# HexadecIcon is a square image showing one hexadecimal per pixel (Grayscale)
#----------------------------------------------------------------------------
set IconInstances 0
oo::class create HexadecIcon {
variable BaseSide
variable ImageName
constructor {Brightness colors} {
global IconInstances
incr IconInstances
set ImageName BaseImage$IconInstances
set BaseSide [string length [lindex $Brightness 0]]
image create photo $ImageName -height [llength $Brightness] -width $BaseSide
$ImageName blank
set y 0
set rgb [split [string toupper $colors] {}]
foreach theRow $Brightness {
set rowLength [string length $theRow]
for {set x 0} {$x < $rowLength} {incr x} {
set value [scan [string index $theRow $x] %x]
foreach {i c} {0 R 1 G 2 B} {
if {$c in $rgb} {
set v$i $value
} else {
set v$i 0
}
}
$ImageName put [format #%x%x%x $v0 $v1 $v2] -to $x $y
}
if {$x != $BaseSide} {
break
}
incr y
}
if {($x != $BaseSide) || ($y != $BaseSide)} {
error "HexadecIcon input must be a geometric square of hexadecimal glyphs."
}
return
}
method getSize {sidePx} {
set factor [gcd $sidePx $BaseSide]
image create photo zoomed
zoomed copy $ImageName -zoom [expr {$sidePx / $factor}]
image create photo sampled
sampled copy zoomed -subsample [expr {$BaseSide / $factor}]
return sampled
}
method map {lo hi result} {
scan $lo$lo %2x decLo
scan $hi$hi %2x decHi
scan $result$result %2x decResult
for {set y 0} {$y < $BaseSide} {incr y} {
for {set x 0} {$x < $BaseSide} {incr x} {
set colors [split [$ImageName get $x $y]]
for {set i 0} {$i < 3} {incr i} {
set value [lindex $colors $i]
if {($decLo <= $value) && ($value <= $decHi)} {
set value $decResult
}
set c$i $value
}
$ImageName put [format #%02x%02x%02x $c0 $c1 $c2] -to $x $y
}
}
return
}
}
#----------------------------------------------------------------------------
# LineItemList is a base class where each list item has no children
#----------------------------------------------------------------------------
oo::class create LineItemList {
variable Constant
constructor {} {
global Prefs
set Constant(descriptionsMax) 0
set Constant(indentMin) -1
set Constant(legend) "LineList"
set Constant(logo) [list "\u2261" black red]
set Constant(structured) false
# Default values follow this line:
set Prefs([my attName descriptions]) 0
set Prefs([my attName indent]) 0
set Prefs([my attName struct]) ""
return
}
method attName {attribute} {
return [string cat $Constant(legend) "_" $attribute]
}
method getConstant {key} {
return $Constant($key)
}
method html {output} {
if {$output eq ""} {
error "No filename provided for html markup of unformatted list"
}
set fp [open $output w]
puts $fp "<html>"
puts $fp [format "<!-- %s -->" [SaveComment $output]]
puts $fp [format "<title>%s</title>" [file tail $output]]
puts $fp "<body>"
my HTMLChildren $fp {}
puts $fp "</body>\n</html>"
close $fp
return
}
method HTMLChildren {fp parentID} {
foreach marking [.f.tvList children $parentID] {
puts $fp [format " <p>%s</p>" [.f.tvList item $marking -text]]
my HTMLChildren $fp $marking
}
return
}
method labelLogo {lblWidget} {
foreach i {"text" "foreground" "background"} j $Constant(logo) {
$lblWidget configure -$i $j
}
return
}
method load {textFile} {
set fp [open $textFile r]
set data [split [read $fp] \n]
close $fp
if {[lindex $data end] eq ""} {
set data [lreplace $data end end]
}
foreach line $data {
Treeview::itemCache [.f.tvList insert {} end -text $line]
}
return 0
}
method save {textFile} {
if {$textFile ne ""} {
set fp [open $textFile w]
my SaveChildren $fp {}
close $fp
}
return
}
method SaveChildren {fp parentID} {
foreach saving [.f.tvList children $parentID] {
puts $fp [format "%s" [.f.tvList item $saving -text]]
my SaveChildren $fp $saving
}
return
}
method structErrorMsg {} {
return "Invalid structure item."
}
method structHelpMsg {} {
return "No structure exists for this list format."
}
method validStruct {hierCode} {
return false
}
}
#----------------------------------------------------------------------------
# Asciidoc implementation of LineItemList methods
#----------------------------------------------------------------------------
oo::class create ADocFormat {
superclass LineItemList
variable Constant
constructor {} {
global Prefs
set Constant(descriptionsMax) 3
set Constant(indentMin) 0
set Constant(legend) "AsciiDoc"
set Constant(logo) [list "AD" white #1F8197]
set Constant(structured) true
# Default values follow this line:
set Prefs([my attName descriptions]) 0
set Prefs([my attName indent]) 4
set Prefs([my attName struct]) "*"
set Selection::tagLegend { \
checkbox "\[ \]/\[x\]" \
priority "_Low_/\*High\*" \
testing "\[ \]/\[Fail\]/\[Pass\]" \
custom "Use text entry contents" }
return
}
method AdocCommentLines {lastSkip lineOfText} {
set result 0
if {$lineOfText eq ""} {
set result 1
} elseif {[regexp {^////} $lineOfText]} {
if {$lastSkip == 10000} {
set result 1
} else {
set result 10000
}
} elseif {[regexp {^//} $lineOfText]} {
set result 1
}
return $result
}
method GetDescToken {trimmed} {
foreach token {"::::" ":::" "::" ";;"} {
set foundDesc [string first $token $trimmed 0]
if {$foundDesc >= 0} {
return $token
}
}
return ""
}
method GetOrderToken {trimmed gap1} {
set token [string range $trimmed 0 [expr {$gap1 - 1}]]
foreach i [split $token {}] {
if {$i ne [string index $token 0]} {
return ""
}
}
return $token
}
method html {output} {
if {$output eq ""} {
error "No filename provided for html markup of asciidoc list"
}
exec asciidoc -o $output $ListFileIO::filename
return
}
method load {textFile} {
global Prefs
set errorAt 0
set prefStruct ""
set levelTag [list]
set skipLines 0
set fp [open $textFile r]
for {set lineNumber 1} {[gets $fp line] >= 0} {incr lineNumber} {
set trimmed [string trimleft $line]
set skipLines [my AdocCommentLines $skipLines $trimmed]
if {$skipLines} {
continue
}
set foundLevel -1
set foundType "+"
set gap1 [string first " " $trimmed]
if {$gap1 > 0} {
###############################################################
# |<--spaces-->|<-----trimmed------>|
# $line: ______________???? alpha beta gamma
# gap1: ------------------^
# token: -->| |<--
###############################################################
set token [my GetOrderToken $trimmed $gap1]
if {$token ne ""} {
set foundLevel [lsearch -exact $levelTag $token]
if {$foundLevel == -1} {
switch -exact -- [string index $token end] {
"." {set foundType "."}
"*" {set foundType "*"}
"-" {set foundType "*"}
}
if {$foundType != "+"} {
set foundLevel [llength $levelTag]
set atStruct [string index $prefStruct $foundLevel]
if {($atStruct eq $foundType) || ($atStruct eq "")} {
lappend levelTag $token
if {$atStruct eq ""} {
append prefStruct $foundType
}
}
}
}
if {$foundLevel >= 0} {
set listItem [string range $trimmed [expr {$gap1 + 1}] end]
}
}
}
if {$foundLevel == -1} {
###############################################################
# |<--spaces-->|<----trimmed----->|
# $line: ______________alpha beta::: gamma
# foundDesc---------------^
# token: -->| |<--
###############################################################
set token [my GetDescToken $trimmed]
if {$token ne ""} {
set foundLevel [lsearch -exact $levelTag $token]
if {$foundLevel == -1} {
set foundType ":"
set foundLevel [llength $levelTag]
set atStruct [string index $prefStruct $foundLevel]
if {($atStruct eq $foundType) || ($atStruct eq "")} {
lappend levelTag $token
if {$atStruct eq ""} {
append prefStruct $foundType
}
}
}
if {$foundLevel >= 0} {
set foundDesc [string first $token $trimmed 0]
set listItem [string range $trimmed 0 [expr {$foundDesc - 1}]]
}
}
}
if {$foundLevel >= 0} {
set foundType [string index $prefStruct $foundLevel]
set levelTag [lrange $levelTag 0 $foundLevel]
}
if {$foundType eq "+"} {
if {![Treeview::loadAppend $trimmed]} {
set errorAt $lineNumber
break
}
} else {
if {[string index $prefStruct $foundLevel] != $foundType} {
set errorAt $lineNumber
break
}
Treeview::loadInsert $foundLevel $listItem
}
}
close $fp
if {!$errorAt} {
set descriptions 0
while {[string index $prefStruct 0] eq ":"} {
incr descriptions
set prefStruct [string range $prefStruct 1 end]
}
if {$prefStruct eq ""} {
set prefStruct "*"
}
set Pref([my attName descriptions]) $descriptions
set Pref([my attName struct]) [Preferences::shortStruct $prefStruct]
}
return $errorAt
}
method save {textFile} {
if {$textFile ne ""} {
set fp [open $textFile w]
puts $fp [SaveComment "//"]
puts $fp ""
my SaveChildren $fp {} 0
close $fp
}
return
}
method SaveChildren {fp parentID depth} {
global Prefs
set indent [string repeat " " $Prefs([my attName indent])]
set depthdent [string repeat $indent $depth]
if {$depth < $Prefs([my attName descriptions])} {
incr depth
set glyphs [string repeat ":" [expr {$depth + 1}]]
foreach saving [.f.tvList children $parentID] {
puts $fp [format "%s%s" [.f.tvList item $saving -text] $glyphs]
my SaveChildren $fp $saving $depth
}
} else {
set adjusted [expr {$depth - $Prefs([my attName descriptions])}]
set glyph [string index $Prefs([my attName struct]) $adjusted]
if {$glyph eq ""} {
set glyph [string index $Prefs([my attName struct]) end]
}
incr depth
set glyphs [string repeat $glyph $depth]
foreach saving [.f.tvList children $parentID] {
puts $fp [format "%s%s %s" $depthdent $glyphs [.f.tvList item $saving -text]]
my SaveChildren $fp $saving $depth
}
}
return
}
method structErrorMsg {} {
return "Invalid structure item: A valid item is \"*\" or \".\""
}
method structHelpMsg {} {
set codes {
"\"*\" \u2192 unordered list type"
"\".\" \u2192 ordered list type"
" "
"Example: \".*\" is ordered list items above unordered lists"
"Note: checklist icons require unordered list"
}
return [join $codes \n]
}
method validStruct {hierCode} {
return [regexp {^[*.]+$} $hierCode]
}
}
#----------------------------------------------------------------------------
# MarkDown implementation of LineItemList methods
#----------------------------------------------------------------------------
oo::class create MDFormat {
superclass LineItemList
variable Constant
constructor {} {
global Prefs
set Constant(descriptionsMax) 0
set Constant(indentMin) 1
set Constant(legend) "MarkDown"
set Constant(logo) [list "M\u2193" black white]
set Constant(structured) true
# Default values follow this line:
set Prefs([my attName descriptions]) 0
set Prefs([my attName indent]) 4
set Prefs([my attName struct]) "*"
set Selection::tagLegend { \
checkbox "\[ \]/\[x\]" \
priority "\*Low\*/\*\*High\*\*" \
testing "\[ \]/\[Fail\]/\[Pass\]" \
custom "Use text entry contents" }
return
}
method GetListToken {line gap1} {
set token [string range $line 0 [expr {$gap1 - 1}]]
if {[string index $token end] eq "."} {
if {[regexp {^(\s*\d*)\.$} $token]} {
regsub -all {\d} $token "" token
}
}
if {[string trimleft $token] in [list "." "+" "-" "*"]} {
return $token
}
return ""
}
method html {output} {
if {$output eq ""} {
error "No filename provided for html markup of markdown list"
}
exec markdown $ListFileIO::filename > $output
return
}
method load {textFile} {
global Prefs
set errorAt 0
set prefStruct ""
set levelTag [list]
set fp [open $textFile r]
for {set lineNumber 1} {[gets $fp line] >= 0} {incr lineNumber} {
set trimmed [string trimleft $line]
if {$trimmed eq ""} {
continue
}
set ws [expr {[string length $line] - [string length $trimmed]}]
set foundType "&"
set foundLevel -1
set gap1 [string first " " $trimmed]
if {$gap1 > 0} {
###############################################################
# |<--spaces-->|<-----trimmed------>|
# $line: ______________???? alpha beta gamma
# gap1: ------------------^
# |<----token----->|
###############################################################
incr gap1 $ws
set token [my GetListToken $line $gap1]
if {$token ne ""} {
set foundLevel [lsearch -exact $levelTag $token]
if {$foundLevel == -1} {
set foundType [string index $token end]
set foundLevel [llength $levelTag]
set atStruct [string index $prefStruct $foundLevel]
if {$atStruct eq $foundType || $atStruct eq ""} {
lappend levelTag $token
if {$atStruct eq ""} {
append prefStruct $foundType
}
}
}
if {$foundLevel >= 0} {
set listItem [string range $line [expr {$gap1 + 1}] end]
}
}
}
if {$foundLevel >= 0} {
set foundType [string index $prefStruct $foundLevel]
set levelTag [lrange $levelTag 0 $foundLevel]
}
if {$foundType eq "&"} {
if {![Treeview::loadAppend $trimmed]} {
set errorAt $lineNumber
break
}
} else {
if {[string index $prefStruct $foundLevel] != $foundType} {
set errorAt $lineNumber
break
}
Treeview::loadInsert $foundLevel $listItem
}
}
close $fp
if {!$errorAt} {
if {$prefStruct eq ""} {
set prefStruct "*"
}
set Pref([my attName struct]) [Preferences::shortStruct $prefStruct]
}
return $errorAt
}
method save {textFile} {
if {$textFile ne ""} {
set fp [open $textFile w]
my SaveChildren $fp {} 0
close $fp
}
return
}
method SaveChildren {fp parentID depth} {
global Prefs
set indent [string repeat " " $Prefs([my attName indent])]
set depthdent [string repeat $indent $depth]
set glyph [string index $Prefs([my attName struct]) $depth]
if {$glyph eq ""} {
set glyph [string index $Prefs([my attName struct]) end]
}
incr depth
set counter 1
foreach saving [.f.tvList children $parentID] {
if {$glyph eq "."} {
puts $fp [format "%s%s%s %s" $depthdent $counter $glyph [.f.tvList item $saving -text]]
} else {
puts $fp [format "%s%s %s" $depthdent $glyph [.f.tvList item $saving -text]]
}
my SaveChildren $fp $saving $depth
incr counter
}
return
}
method structErrorMsg {} {
return "Invalid structure item: A valid item is \"*\", \"+\", \"-\" or \".\""
}
method structHelpMsg {} {
set codes {
"\"*\" \u2192 unordered list type"
"\"+\" \u2192 unordered list type"
"\"-\" \u2192 unordered list type"
"\".\" \u2192 ordered list type"
" "
"Example: \".*\" is ordered list items above unordered lists"
}
return [join $codes \n]
}
method validStruct {hierCode} {
return [regexp {^[*+-.]+$} $hierCode]
}
}
#----------------------------------------------------------------------------
# Maintain list of deleted items to be trashed at program's end
#----------------------------------------------------------------------------
namespace eval Deleted {
proc archiveItems {itemList} {
foreach i $itemList {
set itemText [.f.tvList item $i -text]
if {$itemText ne ""} {
.f.lbDEL insert end $itemText
.f.lbDEL see end
}
archiveItems [.f.tvList children $i]
}
return
}
proc clear {} {
.f.lbDEL delete 0 end
return
}
proc endKey {} {
.f.lbDEL selection clear 0 end
selectPrev
return
}
proc gui {} {
listbox .f.lbDEL -yscrollcommand {.f.sList set}
.f.lbDEL configure -background black -foreground white -selectmode extended
scrollbar .f.sDEL -command {.f.lbDEL yview}
}
proc hide {} {
pack forget .f.lbDEL
pack forget .f.sDEL
return
}
proc homeKey {} {
.f.lbDEL selection clear 0 end
selectNext
return
}
proc returnItem {} {
Gui::setMode "LIST"
foreach i [.f.lbDEL curselection] {
set newItem [Treeview::itemCache [.f.tvList insert {} end -text [.f.lbDEL get $i]]]
.f.tvList selection set $newItem
.f.tvList see [.f.tvList selection]
}
Treeview::focusOn $newItem
return
}
proc selectNext {} {
Navigation::SelectLBItem .f.lbDEL 1
return
}
proc selectPrev {} {
Navigation::SelectLBItem .f.lbDEL -1
return
}
proc show {} {
pack .f.lbDEL -side left -expand yes -fill both
.f.lbDEL configure -yscrollcommand {.f.sDEL set}
update
if {[.f.sDEL get] != {0.0 1.0}} {
pack .f.sDEL -side right -fill y
}
return
}
proc status {} {
set selected [.f.lbDEL curselection]
set prefix ""
if {[llength $selected] > 1} {
set prefix "[llength $selected] of "
} elseif {$selected ne {}} {
set idx [expr {1 + [lindex $selected 0]}]
set prefix [format "%s of " [StatusBar::Suffixed $idx]]
}
set suffix ""
if {$prefix ne ""} {
set suffix " Press <Return> to append selected item to list"
}
StatusBar::show [string cat $prefix "[.f.lbDEL size] deleted item(s)" $suffix]
return
}
}
#----------------------------------------------------------------------------
# Functionality to reformat list content as HTML paragraphs.
#----------------------------------------------------------------------------
namespace eval Export {
proc FormatNonPara {nodeID selected} {
global Prefs
set text [FormatPara $nodeID $selected]
foreach {val start end} { "Bold" "<b>" "</b>"
"Italics" "<i>" "</i>"
"Underline" "<u>" "</u>" } {
if {$Prefs(exportNonPara$val)} {
set text [string cat $start $text $end]
}
}
return [string cat " <br><div>" $text "</div><br>"]
}
proc FormatPara {nodeID selected} {
global Prefs
set sentence [.f.tvList item $nodeID -text]
set tr [string trimright $sentence]
if {$tr ne ""} {
if {$Prefs(exportPeriods)} {
if {[string index $tr end] ne "."} {
set sentence [string cat $tr "."]
}
}
if {$Prefs(exportSentCap)} {
set sentence [string toupper $sentence 0]
}
if {[lsearch -exact $selected $nodeID] ne -1} {
set needle [format "\"background-color:%s\"" \
$Prefs(exportHilight)]
set sentence "<span style=$needle>$sentence</span>"
}
}
return [string cat " " $sentence]
}
proc gui {} {
global Prefs
labelframe .f.export -text "Export" -padx 2 -pady 2
labelframe .f.export.par -pady 2
pack .f.export.par -side top -fill x
Export::GuiPara .f.export.par
labelframe .f.export.non -padx 2 -pady 2
pack .f.export.non -side top -fill x
Export::GuiNonPara .f.export.non
frame .f.export.hilite -padx 2 -pady 2
button .f.export.hilite.btn -text "Set HTML highlight color..." \
-command "SetHighlightColor .f.export.hilite.lbl exportHilight"
label .f.export.hilite.lbl -text "Sample highlight text" \
-background $Prefs(exportHilight)
grid .f.export.hilite.btn .f.export.hilite.lbl -padx 2 -sticky nsew
pack .f.export.hilite -side top -fill x
frame .f.export.buts
ttk::button .f.export.buts.exp -text "Export" -default active -command Export::SaveHTML
ttk::button .f.export.buts.can -text "Cancel" -default normal -command {event generate . <Escape>}
grid .f.export.buts.exp .f.export.buts.can
grid columnconfigure .f.export.buts 0 -weight 1 -uniform a
grid columnconfigure .f.export.buts 1 -weight 1 -uniform a
grid columnconfigure .f.export 2 -weight 1
pack .f.export.buts -side bottom -fill x
frame .f.export.sep -relief groove -borderwidth 2 -width 2 -height 2
pack .f.export.sep -side bottom -fill x -pady 3
Export::LFrameToggle .f.export.non exportNonParagraph
Export::LFrameToggle .f.export.par exportParagraph
return
}
proc GuiNonPara {w} {
global Prefs
set ancestry { "Bold" "Add bold style"
"Italics" "Add italics style"
"Underline" "Add underlined style" }
checkbutton $w.cb -text "Non-Paragraph items" \
-variable Prefs(exportNonParagraph) \
-command "Export::LFrameToggle $w exportNonParagraph"
$w configure -labelwidget $w.cb
label $w.legend -text "Description: Each list item not designated paragraph content will appear on its own line."
pack $w.legend -side top -anchor w
foreach {val legend} $ancestry {
checkbutton $w.exportNonPara$val -text $legend \
-variable Prefs(exportNonPara$val)
pack $w.exportNonPara$val -side top -anchor w
}
return
}
proc GuiPara {w} {
global Prefs
set contents { "parentless" "Leaf items"
"parented" "Single ancestor and leaf items" }
set styles { "block" "Block"
"indent" "Indented" }
checkbutton $w.cb -text "Paragraph items" \
-variable Prefs(exportParagraph) \
-command "Export::LFrameToggle $w exportParagraph"
$w configure -labelwidget $w.cb
label $w.legend -text "Description: Exported sibling leaf nodes (And optionally one ancestor) are catenated."
grid $w.legend -row 0 -column 0 -columnspan 2 -sticky w
labelframe $w.content -text "Content" -padx 2 -pady 2
grid $w.content -row 1 -column 0 -sticky w -padx 2
foreach {val legend} $contents {
radiobutton $w.content.$val -text $legend -value $val \
-variable Prefs(exportParaContent)
pack $w.content.$val -side top -anchor w
}
labelframe $w.style -text "Style" -padx 2 -pady 2
grid $w.style -row 1 -column 1 -sticky w -padx 2
foreach {val legend} $styles {
radiobutton $w.style.$val -text $legend -value $val \
-variable Prefs(exportParaStyle)
pack $w.style.$val -side top -anchor w
}
checkbutton $w.end -variable Prefs(exportPeriods) \
-text "Ensure every exported item ends with period."
grid $w.end -row 2 -column 0 -columnspan 2 -sticky w
checkbutton $w.cap -variable Prefs(exportSentCap) \
-text "Ensure sentence capitalization."
grid $w.cap -row 3 -column 0 -columnspan 2 -sticky w
return
}
proc hide {} {
pack forget .f.export
return
}
proc LFrameToggle {w enable} {
global Prefs
foreach child [winfo children $w] {
if {[llength [winfo children $child]]} {
Export::LFrameToggle $child $enable
} elseif {$child != "$w.cb"} {
if {$Prefs($enable)} {
$child configure -state normal
} else {
$child configure -state disabled
}
}
}
return
}
proc SaveHTML {} {
global Prefs
set targetFile [file rootname $ListFileIO::filename].html
set fp [open $targetFile w]
puts $fp "<html><head>"
puts $fp " <meta charset=\"utf-8\">"
puts $fp [format " <!-- %s -->" [SaveComment $targetFile]]
puts $fp [format " <title>%s</title>" [file tail $targetFile]]
puts $fp " <style type=\"text/css\">"
if {$Prefs(exportParaStyle) eq "indent"} {
puts $fp " p {"
puts $fp " text-indent: 0.5in;"
puts $fp " margin-bottom: 0;"
puts $fp " margin-top: 0;"
puts $fp " }"
}
puts $fp " </style>"
puts $fp "</head>"
puts $fp "<body>"
if {$Prefs(exportNonParagraph) || $Prefs(exportParagraph)} {
Export::SaveHTMLChildren $fp {} [.f.tvList selection]
} else {
puts $fp "<p>NOTICE: No content selected for export</p>"
}
puts $fp "</body>\n</html>"
close $fp
Preferences::save
exec xdg-open $targetFile
return
}
proc SaveHTMLChildren {fp parentID selected} {
global Prefs
set descendents [.f.tvList children $parentID]
if {[llength $descendents] == 0} {
return
}
set state "NONPARAGRAPH"
foreach saving $descendents {
set further [.f.tvList children $saving]
set moreDepth [llength $further]
switch -exact -- $state {
"NONPARAGRAPH" {
if {$moreDepth} {
SaveHTMLNonParagraph $fp $saving $further $selected
} else {
if {$Prefs(exportParagraph)} {
set topicSentence ""
if {$Prefs(exportParaContent) eq "parented"} {
append topicSentence [Export::FormatPara $parentID $selected]
}
puts $fp [format " <p>%s" $topicSentence]
puts $fp [Export::FormatPara $saving $selected]
}
set state "PARAGRAPH"
}
}
"PARAGRAPH" {
if {$moreDepth} {
if {$Prefs(exportParagraph)} {
puts $fp " </p>"
}
set state "NONPARAGRAPH"
SaveHTMLNonParagraph $fp $saving $further $selected
} elseif {$Prefs(exportParagraph)} {
puts $fp [Export::FormatPara $saving $selected]
}
}
}
}
if {($state == "PARAGRAPH") && ($Prefs(exportParagraph))} {
puts $fp " </p>"
}
return
}
proc SaveHTMLNonParagraph {fp parentID further selected} {
global Prefs
if {$Prefs(exportNonParagraph)} {
set printSeparate true
foreach node $further {
if {[llength [.f.tvList children $node]] == 0} {
set printSeparate false
break
}
}
if {$printSeparate || ($Prefs(exportParaContent) eq "parentless")} {