Having some difficulty getting my LaTeX output algorithm right...
The internal representation is, of course, plaintext but with (for simplicity's sake, let us say) bold/italic/smallcap/underline formatting's presence or absence indicating for every character of text.
My first, somewhat naive approach, was something like (I simply considerably below, of course):
Code:
for idx in range(0, len(manuscript.text)):
if manuscript.format[idx-1].bold == True and manuscript.format[idx-1].bold == False:
output += "}"
elif manuscript.format[idx-1].bold == False and manuscript.format[idx-1].bold == True:
output += "\textbf{"
if manuscript.format[idx-1].italic == True and manuscript.format[idx-1].italic == False:
output += "}"
elif manuscript.format[idx-1].italic == False and manuscript.format[idx-1].bold == italic:
output += "\textit{"
output += manuscript.text[idx]
The problem is that the closing braces are not identified with a specific type of opening brace... and the moment formatting is not cleanly nested, it results in incorrect code.
e.g.:
Code:
T h i s i s i n d e e d a s t r a n g e i d e a !
-- -- -- -- -- -I -I -I BI BI B- B- B- B- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
would generate the following LaTeX output from the above algorithm:
Code:
This \textit{is \textbf{in}deed} a strange idea!
resulting in:
This is indeed a strange idea!
instead of the desired:
This is indeed a strange idea!
Basically the \textit's closing brace actually closes the \textbf and vice versa.
---
I have since tried some more complicated variations, but they've either not yielded the correct output, or did so using very ugly (unnecessarily complicated) LaTeX code.
Basically the correct output for the above example would be:
Code:
This \textit{is} \textbf{\textit{in}deed} a strange idea!
.. but (lest somebody suggest just having white-space forcing brace closures and subsequent command reissual when non-white-space characters continue) if the whole word "indeed" were in italic (as well as in bold), the correct output would be:
Code:
This \textit{is \textbf{indeed}} a strange idea!
If anybody can nudge me toward an elegant solution that involves less spaghetti code than I've been throwing at it so far, I'd be most grateful!
Note: Well, not literally spaghetti code, of course... but almost as confusing looking.
- Ahi