@@ -82,11 +82,11 @@ function id(token) {
82
82
let linkedChapter = null , raw = false , quote = false
83
83
84
84
let renderer = {
85
- fence ( token ) {
85
+ fence ( token , _i , _t , newlines ) {
86
86
if ( / \b h i d d e n : \s * t r u e / . test ( token . info ) ) return ""
87
87
let esc = escapeComplexScripts ( token . content . trimRight ( ) )
88
88
if ( noStarch ) esc = esc . replace ( / [ “ ” ] / g, '"' ) . replace ( / … / g, "..." )
89
- return `\n\n ${ id ( token ) } \\begin{lstlisting}\n${ esc } \n\\end{lstlisting}\n`
89
+ return `${ paraBreak ( newlines ) } ${ id ( token ) } \\begin{lstlisting}\n${ esc } \n\\end{lstlisting}\n`
90
90
} ,
91
91
92
92
hardbreak ( ) { return "\\break\n" } ,
@@ -98,24 +98,25 @@ let renderer = {
98
98
return raw ? content : escape ( content )
99
99
} ,
100
100
101
- paragraph_open ( token , i , tokens ) {
101
+ paragraph_open ( token , i , tokens , newlines ) {
102
102
let noIndent = ""
103
103
if ( ! noStarch ) for ( i -- ; i >= 0 ; i -- ) {
104
104
let prev = tokens [ i ]
105
105
if ( prev . type == "fence" ) noIndent = "\\noindent "
106
106
if ( ! / ^ m e t a _ i n d e x / . test ( prev . type ) ) break
107
107
}
108
- let nl = "\n\n" ;
108
+ let nl = paraBreak ( newlines )
109
109
if ( quote ) { nl = "" ; quote = false }
110
110
return nl + noIndent + id ( token )
111
111
} ,
112
112
paragraph_close ( ) { return "" } ,
113
113
114
- heading_open ( token ) {
114
+ heading_open ( token , _i , _t , newlines ) {
115
+ let breaks = paraBreak ( newlines )
115
116
if ( token . tag == "h1" ) return `\\${ ! [ "hints" , "intro" ] . includes ( chapter [ 1 ] ) ? "chapter" : noStarch ? "chapter*" : "addchap" } {`
116
- if ( token . tag == "h2" ) return `\n\n ${ id ( token ) } \\section{`
117
- if ( token . tag == "h3" ) return `\n\n ${ id ( token ) } \\subsection{`
118
- if ( token . tag == "h4" ) return `\n\n ${ id ( token ) } \\subsubsection{`
117
+ if ( token . tag == "h2" ) return `${ breaks } ${ id ( token ) } \\section{`
118
+ if ( token . tag == "h3" ) return `${ breaks } ${ id ( token ) } \\subsection{`
119
+ if ( token . tag == "h4" ) return `${ breaks } ${ id ( token ) } \\subsubsection{`
119
120
throw new Error ( "Can't handle heading tag " + token . tag )
120
121
} ,
121
122
heading_close ( token ) {
@@ -139,7 +140,7 @@ let renderer = {
139
140
tr_open ( ) { return "" } ,
140
141
tr_close ( ) { return "\n\\tabularnewline" } ,
141
142
td_open ( ) { return "\n" } ,
142
- td_close ( _ , next ) { return next && next . type == "td_open" ? " &" : "" } ,
143
+ td_close ( _ , i , tokens ) { return tokens [ i + 1 ] && tokens [ i + 1 ] . type == "td_open" ? " &" : "" } ,
143
144
144
145
code_inline ( token ) {
145
146
if ( noStarch )
@@ -160,11 +161,13 @@ let renderer = {
160
161
sup_open ( ) { return "\\textsuperscript{" } ,
161
162
sup_close ( ) { return "}" } ,
162
163
163
- meta_indexsee ( token ) {
164
- return `\\index{${ escapeIndex ( token . args [ 0 ] ) } |see{${ escapeIndex ( token . args [ 1 ] ) } }}`
164
+ meta_indexsee ( token , _i , _t , newlines ) {
165
+ return paraBreak ( newlines ) +
166
+ `\\index{${ escapeIndex ( token . args [ 0 ] ) } |see{${ escapeIndex ( token . args [ 1 ] ) } }}`
165
167
} ,
166
- meta_index ( token ) {
167
- return token . args . map ( term => `\\index{${ escapeIndex ( term ) } }` ) . join ( "" )
168
+ meta_index ( token , _ , _t , newlines ) {
169
+ return ( token . inline ? "" : paraBreak ( newlines ) ) +
170
+ token . args . map ( term => `\\index{${ escapeIndex ( term ) } }` ) . join ( "" )
168
171
} ,
169
172
170
173
meta_latex_open ( ) { raw = true ; return "" } ,
@@ -184,13 +187,13 @@ let renderer = {
184
187
185
188
inline ( token ) { return renderArray ( token . children ) } ,
186
189
187
- meta_figure ( token ) {
190
+ meta_figure ( token , _i , _t , newlines ) {
188
191
let { url, width, chapter} = token . args [ 0 ]
189
192
if ( chapter ) return "" // FIXME
190
193
if ( / \. s v g $ / . test ( url ) ) url = url . replace ( / ^ i m g \/ / , "img/generated/" ) . replace ( / \. s v g $ / , ".pdf" )
191
194
let graphics = `\\includegraphics[width=${ width || "10cm" } ]{${ url } }`
192
- if ( noStarch ) return `\n\n \\begin{figure}[H]\n${ graphics } \n\\end{figure}\n`
193
- return `\n\n \\vskip 1.5ex\n${ graphics } \n\\vskip 1.5ex\n`
195
+ if ( noStarch ) return `${ paraBreak ( newlines ) } \\begin{figure}[H]\n${ graphics } \n\\end{figure}\n`
196
+ return `${ paraBreak ( newlines ) } \\vskip 1.5ex\n${ graphics } \n\\vskip 1.5ex\n`
194
197
} ,
195
198
196
199
meta_quote_open ( token ) {
@@ -230,12 +233,16 @@ ${image ? `\\includegraphics[width=\\linewidth]{${image}}` : ''}
230
233
meta_hint_close ( ) { return "" }
231
234
}
232
235
236
+ function paraBreak ( newlines ) {
237
+ return "\n" . repeat ( Math . max ( 0 , 2 - newlines ) )
238
+ }
239
+
233
240
function renderArray ( tokens ) {
234
241
let result = ""
235
242
for ( let i = 0 ; i < tokens . length ; i ++ ) {
236
243
let token = tokens [ i ] , f = renderer [ token . type ]
237
244
if ( ! f ) throw new Error ( "No render function for " + token . type )
238
- result += f ( token , i , tokens )
245
+ result += f ( token , i , tokens , / \n * $ / . exec ( result ) [ 0 ] . length )
239
246
}
240
247
return result
241
248
}
0 commit comments