mirror of
https://github.com/atom/atom.git
synced 2026-04-28 03:01:47 -04:00
Handles wrapping text around LaTeX tags
This commit is contained in:
@@ -51,14 +51,30 @@ module.exports =
|
||||
tabLengthInSpaces = ''
|
||||
|
||||
for block in paragraphBlocks
|
||||
blockLines = block.split('\n')
|
||||
|
||||
# For LaTeX tags surrounding the text, we simply ignore them, and
|
||||
# reproduce them verbatim in the wrapped text.
|
||||
beginningLinesToIgnore = []
|
||||
endingLinesToIgnore = []
|
||||
latexTagRegex = /^\s*\\\w+(\[.*\])?\{\w+\}(\[.*\])?\s*$/g # e.g. \begin{verbatim}
|
||||
latexTagStartRegex = /^\s*\\\w+\s*\{\s*$/g # e.g. \item{
|
||||
latexTagEndRegex = /^\s*\}\s*$/g # e.g. }
|
||||
while blockLines[0].match(latexTagRegex) or
|
||||
blockLines[0].match(latexTagStartRegex)
|
||||
beginningLinesToIgnore.push(blockLines[0])
|
||||
blockLines.shift()
|
||||
while blockLines[blockLines.length - 1].match(latexTagRegex) or
|
||||
blockLines[blockLines.length - 1].match(latexTagEndRegex)
|
||||
endingLinesToIgnore.unshift(blockLines[blockLines.length - 1])
|
||||
blockLines.pop()
|
||||
|
||||
# TODO: this could be more language specific. Use the actual comment char.
|
||||
# Remember that `-` has to be the last character in the character class.
|
||||
linePrefix = block.match(/^\s*(\/\/|\/\*|;;|#'|\|\|\||--|[#%*>-])?\s*/g)[0]
|
||||
linePrefix = blockLines[0].match(/^\s*(\/\/|\/\*|;;|#'|\|\|\||--|[#%*>-])?\s*/g)[0]
|
||||
linePrefixTabExpanded = linePrefix
|
||||
if tabLengthInSpaces
|
||||
linePrefixTabExpanded = linePrefix.replace(/\t/g, tabLengthInSpaces)
|
||||
blockLines = block.split('\n')
|
||||
|
||||
if linePrefix
|
||||
escapedLinePrefix = _.escapeRegExp(linePrefix)
|
||||
@@ -93,7 +109,8 @@ module.exports =
|
||||
currentLineLength += segment.length
|
||||
lines.push(linePrefix + currentLine.join(''))
|
||||
|
||||
paragraphs.push(lines.join('\n').replace(/\s+\n/g, '\n'))
|
||||
wrappedLines = beginningLinesToIgnore.concat(lines.concat(endingLinesToIgnore))
|
||||
paragraphs.push(wrappedLines.join('\n').replace(/\s+\n/g, '\n'))
|
||||
|
||||
leadingVerticalSpace + paragraphs.join('\n\n') + trailingVerticalSpace
|
||||
|
||||
|
||||
@@ -560,3 +560,71 @@ describe "Autoflow package", ->
|
||||
'''
|
||||
|
||||
expect(autoflow.reflow(test, wrapColumn: 80)).toEqual res
|
||||
|
||||
it 'properly reflows text around LaTeX tags', ->
|
||||
text =
|
||||
'''
|
||||
\\begin{verbatim}
|
||||
Lorem ipsum dolor sit amet, nisl odio amet, et tempor netus neque at at blandit, vel vestibulum libero dolor, semper lobortis ligula praesent. Eget condimentum integer, porta sagittis nam, fusce vitae a vitae augue. Nec semper quis sed ut, est porttitor praesent. Nisl velit quam dolore velit quam, elementum neque pellentesque pulvinar et vestibulum.
|
||||
\\end{verbatim}
|
||||
'''
|
||||
|
||||
res =
|
||||
'''
|
||||
\\begin{verbatim}
|
||||
Lorem ipsum dolor sit amet, nisl odio amet, et tempor netus neque at at
|
||||
blandit, vel vestibulum libero dolor, semper lobortis ligula praesent. Eget
|
||||
condimentum integer, porta sagittis nam, fusce vitae a vitae augue. Nec
|
||||
semper quis sed ut, est porttitor praesent. Nisl velit quam dolore velit
|
||||
quam, elementum neque pellentesque pulvinar et vestibulum.
|
||||
\\end{verbatim}
|
||||
'''
|
||||
|
||||
expect(autoflow.reflow(text, wrapColumn: 80)).toEqual res
|
||||
|
||||
it 'properly reflows text inside LaTeX tags', ->
|
||||
text =
|
||||
'''
|
||||
\\item{
|
||||
Lorem ipsum dolor sit amet, nisl odio amet, et tempor netus neque at at blandit, vel vestibulum libero dolor, semper lobortis ligula praesent. Eget condimentum integer, porta sagittis nam, fusce vitae a vitae augue. Nec semper quis sed ut, est porttitor praesent. Nisl velit quam dolore velit quam, elementum neque pellentesque pulvinar et vestibulum.
|
||||
}
|
||||
'''
|
||||
|
||||
res =
|
||||
'''
|
||||
\\item{
|
||||
Lorem ipsum dolor sit amet, nisl odio amet, et tempor netus neque at at
|
||||
blandit, vel vestibulum libero dolor, semper lobortis ligula praesent. Eget
|
||||
condimentum integer, porta sagittis nam, fusce vitae a vitae augue. Nec
|
||||
semper quis sed ut, est porttitor praesent. Nisl velit quam dolore velit
|
||||
quam, elementum neque pellentesque pulvinar et vestibulum.
|
||||
}
|
||||
'''
|
||||
|
||||
expect(autoflow.reflow(text, wrapColumn: 80)).toEqual res
|
||||
|
||||
it 'properly reflows text inside nested LaTeX tags', ->
|
||||
text =
|
||||
'''
|
||||
\\begin{enumerate}[label=(\\alph*)]
|
||||
\\item{
|
||||
Lorem ipsum dolor sit amet, nisl odio amet, et tempor netus neque at at blandit, vel vestibulum libero dolor, semper lobortis ligula praesent. Eget condimentum integer, porta sagittis nam, fusce vitae a vitae augue. Nec semper quis sed ut, est porttitor praesent. Nisl velit quam dolore velit quam, elementum neque pellentesque pulvinar et vestibulum.
|
||||
}
|
||||
\\end{enumerate}
|
||||
'''
|
||||
|
||||
res =
|
||||
'''
|
||||
\\begin{enumerate}[label=(\\alph*)]
|
||||
\\item{
|
||||
Lorem ipsum dolor sit amet, nisl odio amet, et tempor netus neque at at
|
||||
blandit, vel vestibulum libero dolor, semper lobortis ligula praesent.
|
||||
Eget condimentum integer, porta sagittis nam, fusce vitae a vitae augue.
|
||||
Nec semper quis sed ut, est porttitor praesent. Nisl velit quam dolore
|
||||
velit quam, elementum neque pellentesque pulvinar et vestibulum.
|
||||
}
|
||||
\\end{enumerate}
|
||||
'''
|
||||
|
||||
expect(autoflow.reflow(text, wrapColumn: 80)).toEqual res
|
||||
|
||||
|
||||
Reference in New Issue
Block a user