Blame


1 c418ae42 2021-02-13 op I just recalled how cool macros are. I was helping to convert a manuscript for a book from LibreOffice to LaTeX, and to speed the conversion we used pandoc. The problem was that pandoc added a lot of noise to the generated code. Take for instance this bit:
2 c418ae42 2021-02-13 op
3 c418ae42 2021-02-13 op ```
4 c418ae42 2021-02-13 op \hypertarget{foo-bar-baz}{%
5 c418ae42 2021-02-13 op \subsubsection[foo bar baz]{\texorpdfstring{\protect\hypertarget{anchor-92}{}{}Foo Bar Baz}{Foo Bar Baz}}\label{foo-bar-baz}}
6 c418ae42 2021-02-13 op ```
7 c418ae42 2021-02-13 op
8 c418ae42 2021-02-13 op that needs to be converted to like
9 c418ae42 2021-02-13 op
10 c418ae42 2021-02-13 op ```
11 c418ae42 2021-02-13 op \section{Foo Bar Baz}
12 c418ae42 2021-02-13 op ```
13 c418ae42 2021-02-13 op
14 c418ae42 2021-02-13 op i.e. subsection → section and remove some crufts. If there were only a handful of those, I could have done it by hand, but given that were around 700 instance of those, it was unfeasible.
15 c418ae42 2021-02-13 op
16 c418ae42 2021-02-13 op My first idea was to fire up sam and play with it a bit. Unfortunately, it’s been a while since I’ve used it extensively, so I’m a bit rusty. The plan was to use the command x to select every paragraph, then g to filter that type of paragraphs and then something with s, but I failed.
17 c418ae42 2021-02-13 op
18 c418ae42 2021-02-13 op Given that I didn’t want to spend too much time on this, I tried to use an Emacs macro. Oh boy, it went so well.
19 c418ae42 2021-02-13 op
20 c418ae42 2021-02-13 op The macro goes along the lines of
21 c418ae42 2021-02-13 op ```
22 c418ae42 2021-02-13 op C-s ;; isearch-forward
23 c418ae42 2021-02-13 op \ ;; self-insert-command
24 c418ae42 2021-02-13 op section ;; self-insert-command * 7
25 c418ae42 2021-02-13 op RET ;; newline
26 c418ae42 2021-02-13 op C-a ;; move-beginning-of-line
27 c418ae42 2021-02-13 op 2*C-M-SPC ;; mark-sexp
28 c418ae42 2021-02-13 op C-w ;; kill-region
29 c418ae42 2021-02-13 op C-p ;; previous-line
30 c418ae42 2021-02-13 op 2*C-o ;; open-line
31 c418ae42 2021-02-13 op C-y ;; yank
32 c418ae42 2021-02-13 op C-n ;; next-line
33 c418ae42 2021-02-13 op M-h ;; mark-paragraph
34 c418ae42 2021-02-13 op C-w ;; kill-region
35 c418ae42 2021-02-13 op ```
36 c418ae42 2021-02-13 op
37 c418ae42 2021-02-13 op it goes to the next \section, select the LaTeX command and the text within the square brackets (using mark-sexp two times), move that bit before and then killing the paragraph.
38 c418ae42 2021-02-13 op
39 c418ae42 2021-02-13 op Then ‘C-- C-x e’ and the whole file was fixed.
40 c418ae42 2021-02-13 op
41 c418ae42 2021-02-13 op The bottom line is, I guess, use your editor and learn it, or something along those lines.