.utils 14.2 KB
Newer Older
Alija Sabic's avatar
Alija Sabic committed
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
pr           = $(shell echo "\e[38;5;36m")
sc           = $(shell echo "\e[38;5;231m")
ac           = $(shell echo "\e[38;5;197m")
rst          = $(shell echo "\e[0m")
dollar       = `echo '\$$'`

out_length   = 80
header       = -------------------------------------------------------------------------------
print_banner = $1$(header)\n==$(rst) $2$1\n$(header)$(rst)\n

ERRS         = error
LOG          = @echo "$(call print_banner,$(ac),Error Log:$(sc)$1$(rst))\e[1A"                               && \
               grep $(ERRS) $1 -nR -A4 -B8 | fold -sw $(out_length)                                           | \
               sed --expression='s/--/$(ac)$(header)\n$(header)$(rst)/g'                                      | \
               grep --color -E -e '' -e '$(ERRS)' && echo "$(ac)$(header)\n$(header)$(rst)\n"

INSTRUCTIONS = "$(call print_banner,$(pr),$(sc)Help Instructions $(rst))\n"                                     \
               "Provided targets:\n\n   build, view, preview, clean, distclean, log, open, help\n"              \
               "\n"                                                                                             \
               "Four environment variables are used to configure the behavior of those targets:\n"              \
               "\n"                                                                                             \
               "$(pr)$(dollar)TARGET$(rst) is used to specify the $(sc)tex$(rst) file to compile"               \
               "(default: $(pr)$(dollar)BASENAME$(rst)).\n"                                                     \
               "$(pr)$(dollar)VARIANTS$(rst) is used to specify none, one, or multiple variants"                \
               "(default: $(pr)$(dollar)BASEVARIANTS$(rst)).\n"                                                 \
               "$(pr)$(dollar)ASPECTRATIO$(rst) is used to specify the aspect ratio(s)"                         \
               "(default: $(pr)$(dollar)BASEARATIO$(rst)).\n"                                                   \
               "$(pr)$(dollar)OPTS$(rst) is used to pass additional options to $(sc)latexmk$(rst).\n"           \
               "\n"                                                                                             \
               "Examples:\n\n"                                                                                  \
               " $(dollar) make $(pr)OPTS$(rst)=$(sc)-gg$(rst)\n"                                               \
               " $(dollar) make $(pr)TARGET$(rst)=$(sc)resources/tex/intro$(rst) view\n"                        \
               " $(dollar) make $(pr)VARIANTS$(rst)=$(sc)handout,presentation$(rst)"                            \
                "$(pr)ASPECTRATIO$(rst)=$(sc)43,169$(rst) build\n"                                              \
               " $(dollar) make $(pr)VARIANTS$(rst)=, view\n"                                                   \
               "\n"                                                                                             \
               "Export variables to set them permanently.\n\n"                                                  \
               " $(dollar) export $(pr)TARGET$(rst)=$(sc)resources/tex/intro.tex$(rst)\n"                       \
               " $(dollar) export $(pr)VARIANTS$(rst)=$(sc)notes$(rst)\n"                                       \
               " $(dollar) export $(pr)ASPECTRATIO$(rst)=$(sc)43$(rst)\n"                                       \
               " $(dollar) make\n"                                                                              \
               " $(dollar) make open\n"                                                                         \
               " $(dollar) make preview\n"                                                                      \
               " $(dollar) make clean\n"                                                                        \
               "\n$(pr)$(header)$(rst)"                                                                         \
               "\n$(pr)$(header)$(rst)"

LATEXMK_ERRS = $(if $(strip $(TARGET)),,$(eval TARGET=$(BASENAME)))                                             \
               $(if $(strip $(VARIANTS)),,$(eval VARIANTS=$(BASEVARIANTS)))                                     \
               $(if $(strip $(ASPECTRATIO)),,$(eval ASPECTRATIO=$(BASEARATIO)))                                 \
               $(foreach file,$(TARGET),                                                                        \
                 $(eval SOURCE=$(notdir $(basename $(file))))                                                   \
                 $(if $(strip $(VARIANTSLIST)),                                                                 \
                   $(foreach variant,$(VARIANTSLIST),                                                           \
                     $(if $(strip $(ASPECTRATIOLIST)),                                                          \
                       $(foreach aratio,$(ASPECTRATIOLIST),                                                     \
                         $(call LOG, $(BUILDDIR)/$(SOURCE)-$(variant)-$(aratio).log)                            \
                        )                                                                                       \
                       ,                                                                                        \
                       $(call LOG, $(BUILDDIR)/$(SOURCE)-$(variant).log)                                        \
                      )                                                                                         \
                    )                                                                                           \
                   ,                                                                                            \
                   $(if $(strip $(ASPECTRATIOLIST)),                                                            \
                     $(foreach aratio,$(ASPECTRATIOLIST),                                                       \
                       $(call LOG, $(BUILDDIR)/$(SOURCE)-$(aratio).log)                                         \
                      )                                                                                         \
                     ,                                                                                          \
                     $(call LOG, $(BUILDDIR)/$(SOURCE).log)                                                     \
                    )                                                                                           \
                  )                                                                                             \
                )

LATEXMK_SELECT = $(if $(strip $(TARGET)),,$(eval TARGET=$(BASENAME)))                                           \
                 $(if $(strip $(VARIANTS)),,$(eval VARIANTS=$(BASEVARIANTS)))                                   \
                 $(if $(strip $(ASPECTRATIO)),,$(eval ASPECTRATIO=$(BASEARATIO)))                               \
                 $(foreach file,$(TARGET),                                                                      \
                   $(eval SOURCE=$(basename $(file)))                                                           \
                   $(if $(strip $(VARIANTSLIST)),                                                               \
                     $(foreach variant, $(VARIANTSLIST),                                                        \
                       $(if $(strip $(ASPECTRATIOLIST)),                                                        \
                         $(foreach aratio, $(ASPECTRATIOLIST),                                                  \
                           export JOBNAME=$(notdir $(SOURCE))-$(variant)-$(aratio)                              \
                                  TYPE=$(variant)                                                               \
                                  ARATIO=$(aratio) &&                                                           \
                           $(call LATEXMK,$(SOURCE),$1);                                                        \
                          )                                                                                     \
                         ,                                                                                      \
                         export JOBNAME=$(notdir $(SOURCE))-$(variant)                                          \
                                TYPE=$(variant)                                                                 \
                                ARATIO=43 &&                                                                    \
                         $(call LATEXMK,$(SOURCE),$1);                                                          \
                        )                                                                                       \
                      )                                                                                         \
                     ,                                                                                          \
                     $(if $(strip $(ASPECTRATIOLIST)),                                                          \
                       $(foreach aratio, $(ASPECTRATIOLIST),                                                    \
                         export JOBNAME=$(notdir $(SOURCE))-$(aratio)                                           \
                                TYPE=presentation                                                               \
                                ARATIO=$(aratio) &&                                                             \
                         $(call LATEXMK,$(SOURCE),$1);                                                          \
                        )                                                                                       \
                       ,                                                                                        \
                       export JOBNAME=$(notdir $(SOURCE))                                                       \
                              TYPE=presentation                                                                 \
                              ARATIO=43 &&                                                                      \
                       $(call LATEXMK,$(SOURCE),$1);                                                            \
                      )                                                                                         \
                    )                                                                                           \
                  )

LATEXMK_PREVIEW = $(if $(strip $(VARIANTS)),,$(eval VARIANTS=$(BASEVARIANTS)))                                  \
                  $(if $(strip $(VARIANTSLIST)),$(eval VARIANTSLIST=$(firstword $(VARIANTSLIST))),)             \
                  $(call LATEXMK_SELECT, -pvc)

LATEXMK_OPEN    = $(if $(strip $(TARGET)),,$(eval TARGET=$(BASENAME)))                                          \
                  $(if $(strip $(VARIANTS)),,$(eval VARIANTS=$(BASEVARIANTS)))                                  \
                  $(if $(strip $(ASPECTRATIO)),,$(eval ASPECTRATIO=$(BASEARATIO)))                              \
                  $(foreach file,$(TARGET),                                                                     \
                    $(eval SOURCE=$(basename $(file)))                                                          \
                    $(if $(strip $(VARIANTSLIST)),                                                              \
                      $(foreach variant, $(VARIANTSLIST),                                                       \
                        $(if $(strip $(ASPECTRATIOLIST)),                                                       \
                          $(foreach aratio, $(ASPECTRATIOLIST),                                                 \
                            $(eval JOBNAME=$(notdir $(SOURCE))-$(variant)-$(aratio).pdf)                        \
                            $(VIEWER) $(BUILDDIR)/$(JOBNAME);                                                   \
                           )                                                                                    \
                          ,                                                                                     \
                          $(eval JOBNAME=$(notdir $(SOURCE))-$(variant).pdf)                                    \
                          $(VIEWER) $(BUILDDIR)/$(JOBNAME);                                                     \
                        )                                                                                       \
                       )                                                                                        \
                      ,                                                                                         \
                      $(if $(strip $(ASPECTRATIOLIST)),                                                         \
                       $(foreach aratio, $(ASPECTRATIOLIST),                                                    \
                         $(eval JOBNAME=$(notdir $(SOURCE))-$(aratio).pdf)                                      \
                         $(VIEWER) $(BUILDDIR)/$(JOBNAME);                                                      \
                        )                                                                                       \
                       ,                                                                                        \
                       $(eval JOBNAME=$(notdir $(SOURCE)).pdf)                                                  \
                       $(VIEWER) $(BUILDDIR)/$(JOBNAME);                                                        \
                       )                                                                                        \
                     )                                                                                          \
                   )