-
Notifications
You must be signed in to change notification settings - Fork 3
/
htmlviewer
executable file
·3338 lines (2898 loc) · 87.9 KB
/
htmlviewer
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
#!/usr/bin/env bash
set -e
inputs='# default input.conf
space pgdown_or_next
shift+space pgup_or_prev
space restrict webtoon pgdown_or_next 98
space restrict all pgdown_or_next 98
shift+space restrict webtoon pgup_or_prev 98
shift+space restrict all pgup_or_prev 98
z next -5
x next 5
a next -12
s next 12
q next -30
w next 30
e next -100
r next 100
left prev
right next
ctrl+left next -5
ctrl+right next 5
ctrl+up next 12
ctrl+down next -12
< next_chap -1
> next_chap 1
| prev
\ next
c pgup_or_prev
v prev
b pgup_or_prev 50
n pgdown_or_next 50
. prev
/ pgdown_or_next
m rotate -90
, rotate 90
g goto
^ goto 0
$ goto -1
h mode webtoon
j mode double
k mode normal
l mode normal_or_double
d info
o info
t toggle_auto_scroll
u add_interval -25
i add_interval 25
y newtab
Y newtab foreground
# Mouse shortcuts
left_click pgdown_or_next
shift+left_click pgup_or_prev
ctrl+left_click next_chap
shift+ctrl+left_click next_chap -1
alt+left_click newtab
altgr+left_click newtab
@ mouse_assistance
# extra
F1 shortcuts
F2 shortcuts
F8 search
F10 open
F6 save
F7 load
'
config="${XDG_CONFIG_DIRS:-${XDG_DATA_HOME:-$HOME}/.config}"/htmlviewer
inputconf="$config"/input.conf
triggered=''
co=$'\x1b[31m' # option name
cv=$'\x1b[32m' # parameter name (cf. <xxxx>)
cl=$'\x1b[34m' # list of parameter values (cf. {xxx|yyy})
cd=$'\x1b[37m' # default value
cr=$'\x1b[m' # reset color
usage() {
echo "Generate a html viewer file
htmlviewer [$co-a$cr|$co-A$cr] [$co-n$cr] [$co-s$cr] [$co-S$cr] [$co-c$cr <${cv}command${cr}>] [$co-e$cr <${cv}html${cr}>] [$co-f$cr <${cv}filename${cr}>]
[$co-i$cr <${cv}filename${cr}>] [$co-m$cr {${cl}normal${cr}|${cl}double${cr}|${cl}normal_or_doube${cr}|${cl}webtoon${cr}|${cl}all${cr}}]
[$co-t$cr <${cv}title${cr}>] [$co-x$cr <${cv}pixel_distance${cr}>] directory...
htmlviewer $co-p$cr
htmlviewer $co-K$cr
htmlviewer $co-h$cr"
}
help() {
echo "
$co-a$cr
Builds a web page that can open images from the viewer.
$co-A$cr
Builds a web page that lists the shortcuts available for the current
configuration.
$co-c$cr <${cv}command${cr}>
Add a command. See input.conf syntax.
$co-e$cr <${cv}html${cr}>
Specify content of html header. By default its contains the html style.
$co-f$cr <${cv}filename${cr}>
Read directories from <${cv}filename${cr}>. If - is specified as filename,
directories are read from the standard input.
find -type d | fzf | htmlviewer -f- > view.html
$co-i$cr <${cv}filename${cr}>
Specify configuration file. See input.conf.
$co-h$cr
Show this help.
$co-K$cr
Generates a html file which displays the name of the keys and
mouse buttons.
$co-m$cr {${cl}normal${cr}|${cl}double${cr}|${cl}normal_or_double${cr}|${cl}webtoon${cr}|${cl}all${cr}}
Specify default mode.
$co-n$cr
Do not load default configuration files.
$co-p$cr
Print the default input.conf file.
$co-s$cr
Sort input directories (default).
$co-S$cr
Do not sort input directories.
$co-t$cr <${cv}title${cr}>
Specify title page.
$co-x$cr <${cv}pixel_distance${cr}>
Specify default value for bottom_pixel command.
input.conf
By default load $cd\${XDG_CONFIG_DIRS}/htmlviewer/input.conf$cr with
$cd\$HOME/.config$cr as default value when XDG_CONFIG_DIRS is unset.
Currently $cd$inputconf$cr
The input.conf file consists of a list of mouse and key bindings,
for example:
n next # go to the next image with the n key
m rotate 90 # rotates images 90 degrees
Each line maps a key to an input command.
In general, keys can be combined with ${cl}Shift${cr}, ${cl}Ctrl${cr}, ${cl}Alt${cr}, ${cl}Meta${cr} and ${cl}AltGr${cr}
(Modifiers are case insensitive):
shift+Space pgup_or_prev
${cl}AltGr${cr} and ${cl}Shift${cr} are optional.
# the following 2 commands are equivalent
shift+y newtab
Y newtab
input.conf syntax
<${cv}key${cr}> <${cv}command${cr}> [<${cv}args${cr}>...]
Newlines always start a new binding. # starts a comment.
To bind commands to the # key, SHARP can be used.
<${cv}key${cr}> is either the literal character the key produces (ASCII,
Unicode character or Javascript KeyEvent.key). See ${co}-K${cr} flag.
<${cv}command${cr}> is the command itself. It consists of the command name
and multiple (or none) arguments, all separated by whitespace.
Special key names
${cl}space${cr}
${cl}sharp${cr} for #
${cl}plus${cr} alias for +
${cl}pgup${cr} alias for PageUp
${cl}pgdown${cr} alias for PageDown
${cl}up${cr} alias for ArrowUp
${cl}down${cr} alias for ArrowDown
${cl}left${cr} alias for ArrowLeft
${cl}right${cr} alias for ArrowRight
${cl}left_click${cr} for click0
${cl}middle_click${cr} for click1
${cl}right_click${cr} for click2
Note: special names are case insensitive.
Commands
${co}next${cr} [${cv}n${cr}=${cd}1${cr}]
Advance n images. A negative number goes back ${cv}n${cr} images.
${co}prev${cr} [${cv}n${cr}=${cd}1${cr}]
Equivalent to next -${cv}n${cr}.
${co}next_chap${cr} [${cv}n${cr}=${cd}1${cr}]
Advance n directories. A negative number goes back ${cv}n${cr} directories.
${co}prev_chap${cr} [${cv}n${cr}=${cd}1${cr}]
Equivalent to next_chap -${cv}n${cr}.
${co}pgup${cr} [${cv}percent${cr}=${cd}100${cr}]
${co}pgdown${cr} [${cv}percent${cr}=${cd}100${cr}]
${co}pgdown_or_next${cr} [${cv}percent${cr}=${cd}100${cr}] [${cv}pixel_distance${cr}=${cd}\$bottom_pixel${cr}]
Next image when at the bottom of the page, otherwise scroll down.
${co}pgup_or_prev${cr} [${cv}percent${cr}=${cd}100${cr}]
Previous image when at the top of the page, otherwise scroll up.
${co}goto${cr} [${cv}image_position${cr}]
Opens a page selection window.
When '${cv}image_position${cr}' is specified, the nth image is opened directly.
A negative number begins at the end and too large a number selects
the last frame.
${co}rotate${cr} <${cv}deg${cr}>
Rotate an images.
${co}newtab${cr} [{${cl}background${cr}|${cl}foreground${cr}|${cl}window${cr}}]
Open the current image in a background tab (default), foreground tab
or new window.
Keyboard shortcuts containing ctrl or alt may malfunction with this
option. No problem with mouse shortcuts.
window depends on browser options and may open a foreground tab.
${co}mode${cr} {${cl}normal${cr}|${cl}double${cr}|${cl}normal_or_double${cr}|${cl}webtoon${cr}|${cl}all${cr}} [${cv}image_limit${cr}=${cd}0${cr}]
- ${cl}normal${cr}: One image at a time.
- ${cl}double${cr}: Once on 2, the commands ${co}pgdown_or_next${cr}/${co}pgup_or_prev${cr} go to
top/bottom of the page rather than go to the next/prev
image.
- ${cl}normal_or_double${cr}: Use double page mode for images whose width is
greater than the height, otherwise use the normal
mode.
- ${cl}webtoon${cr}: All images in a chapter are displayed one below the other.
- ${cl}all${cr}: All images are displayed one below the other.
'${cv}image_limit${cr}' specifies the maximum number of images displayed per
page. ${cv}0${cr} for no limit.
${co}info${cr}
Show current image position.
${co}auto_scroll${cr} [${cv}ms_delay${cr}=${cd}800${cr}]
Start auto scroll with specific delay.
${co}toggle_auto_scroll${cr} [${cv}ms_delay${cr}=${cd}800${cr}]
Resume or stop auto scroll. Equivalent to ${co}auto_scroll${cr} when not started.
${co}add_interval${cr} <${cv}ms_delay${cr}>
Add delay of auto scroll. Delay can be negative.
${co}set_bottom_pixel${cr} <${cv}pixel_distance${cr}>
Number of pixels at the bottom of the page that can be skipped for
so that ${co}pgdown_or_next${cr}, ${co}auto_scroll${cr} and ${co}toggle_auto_scroll${cr} goes to
the next image.
Default is ${cd}20${cr}.
${co}add_bottom_pixel${cr} <${cv}pixel_distance${cr}>
Add ${cv}pixel_distance${cr}. See ${co}set_bottom_pixel${cr}.
${co}trigger${cr} [${cv}ms_delay${cr}=${cd}0${cr}] <${cv}command${cr}>
Allows to trigger <${cv}command${cr}> when opening the page after <${cv}ms_delay${cr}>.
The keyboard shortcut is associated with <${cv}command${cr}>.
You can put a key that does not exist to make no association:
nokey trigger 200 auto_scroll 600
h trigger mode webtoon
# is equivalent to
nokey trigger mode webtoon
h mode webtoon
${co}mouse${cr} {${cl}top${cr}|${cl}bottom${cr}|${cl}left${cr}|${cl}right${cr}|${cl}top_left${cr}|${cl}top_right${cr}|${cl}bottom_left${cr}|${cl}bottom_right${cr}} <${cv}command${cr}>
Execute <${cv}command${cr}> only when the mouse pointer is in a certain area.
Note: Only works with mouse shortcuts.
${co}mouse_assistance${cr}
Enable or disable the display of mouse area.
${co}restrict${cr} {${cl}normal${cr}|${cl}double${cr}|${cl}normal_or_double${cr}|${cl}webtoon${cr}|${cl}all${cr}} <${cv}command${cr}>
Enable a shortcut for a specific mode.
${co}shortcuts${cr}
Show shortcuts.
${co}open${cr}
Open directories.
${co}search${cr} [{${cl}chapter${cr}|${cl}image${cr}|${cl}chapter_then_image${cr}}
|{${cl}case_sensitive${cr}|${cl}case_insensitive${cr}|${cl}uppercase_only${cr}}
|{${cl}accent_sensitive${cr}|${cl}accent_insensitive${cr}|${cl}accent_only${cr}}
|{${cl}word${cr}|${cl}fuzzy${cr}|${cl}text${cr}|${cl}regex${cr}}
]...
Show a finder.
In the input field, use the ${cd}up${cr}/${cd}down${cr} arrow and ${cd}page up${cr}/${cd}down${cr} to
navigate the search list. ${cd}Enter${cr} validates selection.
The default options are ${cd}chapter_then_image${cr}, ${cd}uppercase_only${cr},
${cd}accent_only${cr} and ${cd}word${cr}.
- ${cl}chapter${cr}, ${cl}image${cr}, ${cl}chapter_then_image${cr}:
Indicates on which part of the url the search is performed.
${cl}chapter${cr} and ${cl}image${cr} can be abbreviated to ${cl}chap${cr} and ${cl}img${cr}.
- ${cl}case_sensitive${cr}, ${cl}case_insensitive${cr}, ${cl}uppercase_only${cr}:
Indicates whether upper and lower case letters should be
considered the same.
'${cl}uppercase_only${cr}' searches case insensitive for lowercase letters
and case sensitive for uppercase letters. Does not work with
regex mode.
- ${cl}accent_sensitive${cr}, ${cl}accent_insensitive${cr}, ${cl}accent_only${cr}:
Indicates whether accents should be ignored.
'${cl}accent_only${cr}' ignore accent for unaccented letters and match
accent for accented letters. Does not work with regex mode.
- ${cl}word${cr}, ${cl}fuzzy${cr}, ${cl}text${cr}, ${cl}regex${cr}:
Algorithm used for the filter.
${co}save${cr}
Save position and mode.
${co}load${cr}
Load saved position and mode.
COPYRIGHT
By Jonathan Poelen
MIT License
https://github.com/jonathanpoelen/manga-viewer
"
}
declare -A fns=(
[next]=1
[prev]=1
[next_chap]=1
[prev_chap]=1
[mode]=1
[pgup]=1
[pgdown]=1
[goto]=1
[rotate]=1
[newtab]=1
[pgdown_or_next]=1
[pgup_or_prev]=1
[info]=1
[toggle_auto_scroll]=1
[auto_scroll]=1
[add_interval]=1
[set_bottom_pixel]=1
[add_bottom_pixel]=1
[trigger]=1
[mouse]=1
[mouse_assistance]=1
[restrict]=1
[shortcuts]=1
[open]=1
[search]=1
[save]=1
[load]=1
)
declare -A remap=(
[space]=' '
[left]='ArrowLeft'
[right]='ArrowRight'
[up]='ArrowUp'
[down]='ArrowDown'
[pgup]='PageUp'
[pgdown]='PageDown'
[sharp]='#'
[plus]='+'
[left_click]='click0'
[middle_click]='click1'
[right_click]='click2'
['\']='\\'
[\']=\\\'
)
declare -A show_shortcut_map=(
[ArrowLeft]=Left
[ArrowRight]=Right
[ArrowUp]=Up
[ArrowDown]=Down
[click0]=LeftClick
[click1]=MiddleClick
[click2]=RightClick
)
declare -A mousezone=(
[xtop]=1
[xbottom]=2
[xleft]=4
[xright]=8
[xtop_left]=5
[xleft_top]=5
[xright_top]=9
[xtop_right]=9
[xbottom_left]=6
[xleft_bottom]=6
[xbottom_right]=10
[xright_bottom]=10
[xall]=15
)
check_n()
{
if (( $2 != $1 )); then
error="requires $1 argument, $2 but were provided"
return 1
fi
}
check_int()
{
local -i n=$1
if [[ "$1" != "$n" ]]; then
error="bad argument: $1 is not a number"
return 1
fi
}
check_positive()
{
local -i n=$1
if [[ "$1" != "$n" ]] || (( $n < 0 )); then
error="bad argument: $1 is not a positive number"
return 1
fi
}
init_local_mode()
{
case "$1" in
normal) mode=normalMode;;
double) mode=doublePageMode;;
webtoon) mode=webtoonMode;;
normal_or_double) mode=normalOrDoublePageMode;;
all) mode=allMode;;
*) error="Unknown mode '$1'";
return 1;;
esac
}
subcomand()
{
local fn=$1
if [[ -z ${fns[$fn]} ]]; then
error="Unknown command '$fn'"
return 1
fi
shift
cmd_$fn "$@"
}
cmd_next()
{
if (( $# == 0 )); then
code='mode.advance(1);'
else
check_n 1 $#
check_int "$1"
code="mode.advance($1);"
fi
}
cmd_prev()
{
if (( $# == 0 )); then
code='mode.advance(-1);'
else
check_n 1 $#
check_int "$1"
local -i n=$1
((n=-n))
code="mode.advance($n);"
fi
}
cmd_next_chap()
{
if (( $# == 0 )); then
code='mode.advanceChap(1);'
else
check_n 1 $#
check_int "$1"
code="mode.advanceChap($1);"
fi
}
cmd_prev_chap()
{
if (( $# == 0 )); then
code='mode.advanceChap(-1);'
else
check_n 1 $#
check_int "$1"
local -i n=$1
((n=-n))
code="mode.advanceChap($n);"
fi
}
cmd_goto()
{
if (( $# == 0 )); then
code='showGoto();'
else
check_n 1 $#
check_int "$1"
code="gotoImage($1);"
fi
}
cmd_rotate()
{
check_n 1 $#
check_int "$1"
code="rotate($1);"
}
cmd_newtab()
{
if (( $# == 0 )); then
code='openInNewTab(0);'
else
check_n 1 $#
case "$1" in
window) code="openInNewTab(2);";;
foreground) code="openInNewTab(1);";;
background) code="openInNewTab(0);";;
*) error="Unknown '$1'"; return 1;;
esac
fi
}
cmd_mode()
{
local mode image_limit=0
init_local_mode "$1"
if (( $# > 1 )); then
check_n 2 $#
check_int "$2"
image_limit="$2"
fi
code="selectMode($mode, $image_limit);"
}
cmd_pgup_or_prev()
{
if (( $# == 0 )); then
code='upOrPrev(body.clientHeight);'
else
check_n 1 $#
check_int "$1"
code="upOrPrev(body.clientHeight * ($1 / 100));"
fi
}
cmd_pgdown_or_next()
{
if (( $# == 0 )); then
code='downOrNext(bottomPixelDistance, body.clientHeight);'
elif (( $# == 1 )); then
check_int "$1"
code="downOrNext(bottomPixelDistance, body.clientHeight * ($1 / 100));"
else
check_n 2 $#
check_int "$1"
check_positive "$2"
code="downOrNext($2, body.clientHeight * ($1 / 100));"
fi
}
cmd_pgup()
{
if (( $# == 0 )); then
code='body.scrollTop -= body.clientHeight;'
else
check_n 1 $#
check_int "$1"
code="body.scrollTop -= body.clientHeight * ($1 / 100);"
fi
}
cmd_pgdown()
{
if (( $# == 0 )); then
code='body.scrollTop += body.clientHeight;'
else
check_n 1 $#
check_int "$1"
code="body.scrollTop += body.clientHeight * ($1 / 100);"
fi
}
cmd_info()
{
check_n 0 $#
code='showImageInfo();'
}
cmd_auto_scroll()
{
if (( $# == 0 )); then
code='startAutoScroll(800);' ;
else
check_n 1 $#
check_positive "$1"
code="startAutoScroll($1);"
fi
}
cmd_toggle_auto_scroll()
{
if (( $# == 0 )); then
code='toggleAutoScroll(800);' ;
else
check_n 1 $#
check_positive "$1"
code="toggleAutoScroll($1);"
fi
}
cmd_add_interval()
{
check_n 1 $#
check_int "$1"
code="addAutoScrollInterval($1);"
}
cmd_set_bottom_pixel()
{
check_n 1 $# &&
check_positive "$1" &&
code="bottomPixelDistance = $1;"
}
cmd_add_bottom_pixel()
{
check_n 1 $#
check_int "$1"
code="bottomPixelDistance = max(0, bottomPixelDistance + $1);"
}
cmd_trigger()
{
local -i delay=$1
[[ "$1" != "$delay" ]] && delay=0 || shift
subcomand "$@"
triggered+="{action: function(){$code}, delay: $delay},"$'\n'
}
cmd_mouse()
{
local zone=${mousezone["x$1"]}
if [[ -z $zone ]]; then
error="Unknown zone '$1'"
return 1
fi
shift
subcomand "$@"
code="if (isInZone(arguments[0], $zone)) { $code }"
}
cmd_mouse_assistance()
{
check_n 0 $#
code='toggleMouseAssistance()'
}
cmd_restrict()
{
init_local_mode "$1"
shift
subcomand "$@"
}
cmd_shortcuts()
{
check_n 0 $#
code='showShorcuts();'
}
cmd_open()
{
check_n 0 $#
code='showLoader();'
}
cmd_search()
{
local i
code='showSearcher((SEARCH_BY_CHAPTER_THEN_BY_IMAGE | SEARCH_UPPERCASE_ONLY | SEARCH_ACCENT_ONLY | SEARCH_MODE_WORD)'
while (($# != 0)); do
case "${1//-/_}" in
chap|chapter) code+=' & (~SEARCH_BY_MASK | SEARCH_BY_CHAPTER)';;
img|image) code+=' & (~SEARCH_BY_MASK | SEARCH_BY_IMAGE)';;
chap_then_img|chapter_then_image|chapter_then_img|chap_then_image)
code+=' & (~SEARCH_BY_MASK | SEARCH_BY_CHAPTER_THEN_BY_IMAGE)';;
case_sensitive) code+=' & (~SEARCH_CASE_SENSITIVITY_MASK | SEARCH_CASE_SENSITIVE)';;
case_insensitive) code+=' & (~SEARCH_CASE_SENSITIVITY_MASK | SEARCH_CASE_INSENSITIVE)';;
uppercase_only) code+=' & (~SEARCH_CASE_SENSITIVITY_MASK | SEARCH_UPPERCASE_ONLY)';;
accent_sensitive) code+=' & (~SEARCH_ACCENT_SENSITIVITY_MASK | SEARCH_ACCENT_SENSITIVE)';;
accent_insensitive) code+=' & (~SEARCH_ACCENT_SENSITIVITY_MASK | SEARCH_ACCENT_INSENSITIVE)';;
accent_only) code+=' & (~SEARCH_ACCENT_SENSITIVITY_MASK | SEARCH_ACCENT_ONLY)';;
word) code+=' & (~SEARCH_MODE_MASK | SEARCH_MODE_WORD)';;
fuzzy) code+=' & (~SEARCH_MODE_MASK | SEARCH_MODE_FUZZY)';;
text) code+=' & (~SEARCH_MODE_MASK | SEARCH_MODE_TEXT)';;
regex) code+=' & (~SEARCH_MODE_MASK | SEARCH_MODE_REGEX)';;
*) error="Unknown '$1' option";
return 1;;
esac
shift
done
code+=');'
}
cmd_save() {
check_n 0 $#
code='savePosition();'
}
cmd_load() {
check_n 0 $#
code='loadPosition();'
}
minihelp()
{
usage
echo
echo "input.conf path: $cd$inputconf$cr"
if [[ ! -f "$inputconf" ]]; then
echo " But not found, a default configuration will be used (see $co-p$cr)."
fi
echo
echo "Use $co-a$cr for create a view without directory. Directories to be loaded"
echo 'will be asked when opening the html file.'
echo
echo "Use $co-h$cr for more information."
}
typeset -i standalone=0 sort=1 hasinput=0
inputdirs=''
htmlheader=
force_mode_code=
force_pixel_distance_code=
title=
help_shortcut=
pgdown_or_next_shortcut=
pgup_or_prev_shortcut=
declare -a cmds=()
while getopts 'aAsSnpKi:c:e:t:f:x:m:h?' opt ; do
case $opt in
s) sort=1;;
S) sort=0;;
n) inputconf=''
hasinput=0;;
i) inputconf="$OPTARG"
hasinput=1;;
e) htmlheader="$OPTARG";;
c) cmds+=($OPTIND "$OPTARG");;
t) title="$OPTARG";;
f) inputdirs="$OPTARG";;
m) cmd_mode $OPTARG && force_mode_code=$code || {
echo "arg $OPTIND: -m $OPTARG: $error" >&2
exit 2
};;
x) cmd_set_bottom_pixel "$OPTARG" && force_pixel_distance_code="$OPTARG" || {
echo "arg $OPTIND: -x $OPTARG: $error" >&2
exit 2
};;
p) echo -n "$inputs";
exit;;
a) standalone=1;;
A) standalone=2;;
h) usage; help; exit;;
K) echo '<!DOCTYPE html>
<html><head><title>Key viewer</title><meta charset="Utf-8"/></head>
<noscript><p>Javascript must be enabled</p></noscript>
<p>Press a keyboard key or mouse click to see the associated values.</p>
<div id="output"></div>
<script>const output = document.getElementById("output");
window.addEventListener("keydown", function(event) {
const p = document.createElement("p");
p.textContent = `key="${event.key}"`;
output.appendChild(p);
}, true);
function mouseEvent(event) {
const p = document.createElement("p");
p.textContent = `key="click${event.button}"`;
output.appendChild(p);
};
window.addEventListener("click", mouseEvent, true);
window.addEventListener("auxclick", mouseEvent, true);
</script></body></html>';
exit;;
?) minihelp >&2;
exit 2;;
esac
done
shift $(($OPTIND - 1))
if (( $# == 0 )) && (( ! $standalone )); then
minihelp >&2
exit 1
fi
if [[ -z "$title" ]]; then
if (( $standalone == 2 )); then
title='Shortcut list for manga-viewer / htmlviewer'
else
title="Directories: $@"
title="${title:0:200}"
fi
fi
shortcuts=()
html_table_shortcuts=()
declare -A shortcuts_by_mode=()
parse_input_line()
{
local key=$1
[[ ${key[0]} != '#' ]] || return
local fn=$2
[[ ${fn[0]} != '#' ]] || {
error="key without command"
return 1
}
if [[ -z ${fns[$fn]} ]]; then
error="Unknown command '$fn'"
return 1
fi
shift 2
# strip comment
local -a args=()
for arg in "$@"; do
[[ ${arg[0]} != '#' ]] || break
args+=("$arg")
done
local mode # set by restrict
cmd_$fn "${args[@]}" || error="$fn: $error"
# extract controls and convert key to lowercase
local -A controls=([alt]=0 [ctrl]=0 [shift]=0 [meta]=0 [altgr]=0)
local ifs="$IFS"
IFS=+
local -a keys=($key)
IFS="$ifs"
local lowerkey
for key in "${keys[@]}"; do
lowerkey="${key,,}"
controls["${lowerkey}"]=1
done
local newkey="${remap[${lowerkey}]}"
[[ -z $newkey ]] || key="$newkey"
local shortcut="{alt: ${controls[alt]}, ctrl: ${controls[ctrl]}, shift: ${controls[shift]}, meta: ${controls[meta]}, altgr: ${controls[altgr]}, key: '$key', action: function(){ $code }},"$'\n'
if [[ -z $mode ]]; then
shortcuts+=("$shortcut")
else
shortcuts_by_mode[$mode]+="$shortcut"
fi
# shortcut for help
if [[ $key != nokey ]]; then
local display_key="${key}"
if (( ${#key} > 1 )); then
display_key="${show_shortcut_map[$key]}"
if [[ -z $display_key ]]; then
display_key="${key^}"
fi
else
# html escape
display_key="${display_key/ /Space}"
display_key="${display_key/&/'&'}"
display_key="${display_key/</'<'}"
fi
local cmdraw
# remove trigger prefix
if [[ $fn = trigger ]]; then
# remove delay prefix
if [[ ${args[@]} = [0-9] ]]; then
local tmp=("${args[@]}")
tmp[0]=''
cmdraw="${tmp[@]}"
else
cmdraw="${args[@]}"
fi
else
cmdraw="$fn ${args[@]}"
fi
[[ 0 != ${controls[meta]} ]] && display_key="Meta+$display_key"
[[ 0 != ${controls[altgr]} ]] && display_key="AltGr+$display_key"
[[ 0 != ${controls[shift]} ]] && display_key="Shift+$display_key"
[[ 0 != ${controls[alt]} ]] && display_key="Alt+$display_key"
[[ 0 != ${controls[ctrl]} ]] && display_key="Ctrl+$display_key"
html_table_shortcuts+=("<tr><td>$display_key</td><td>$cmdraw</td></tr>")
if [[ -z $mode ]]; then
if [[ $fn == 'shortcuts' ]]; then
help_shortcut+="<code>$display_key</code>, "
elif [[ $fn == 'pgdown_or_next' ]]; then
pgdown_or_next_shortcut+="<code>$display_key</code>, "
elif [[ $fn == 'pgup_or_prev' ]]; then
pgup_or_prev_shortcut+="<code>$display_key</code>, "
fi
fi
fi
}
# read input.conf
if [[ -f "$inputconf" ]]; then
inputs="$(<$inputconf)"
elif (( $hasinput )); then
echo "Unknown '$inputconf'" >&2
exit 1
fi
errors=''
# parse input.conf
nline=0
while read -r line ; do
((++nline))
[[ -n $line ]] || continue
error=
parse_input_line $line ||:
[[ -z $error ]] || errors+="line $nline: $error in '$line'"$'\n'
done <<<"$inputs"
# parse -c options
for ((i=0; i < ${#cmds[@]}; i+=2)); do
error=
cmd="${cmds[$((i+1))]}"
parse_input_line $cmd ||:
[[ -z $error ]] || errors+="arg ${cmds[$i]}: $error in '$cmd'"$'\n'
done