1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Miscellaneous utilities
4 // Author: Julian Smart
5 // Modified by: Wlodzimiez ABX Skiba 2003/2004 Unicode support
9 // Copyright: (c) Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
26 #include "wx/textfile.h"
44 static inline wxChar
* copystring(const wxChar
* s
)
45 { return wxStrcpy(new wxChar
[wxStrlen(s
) + 1], s
); }
47 wxHashTable
TexReferences(wxKEY_STRING
);
48 wxList
BibList(wxKEY_STRING
);
49 wxStringList CitationList
;
50 wxList
ColourTable(wxKEY_STRING
);
51 wxHashTable
BibStringTable(wxKEY_STRING
);
52 wxList
CustomMacroList(wxKEY_STRING
);
53 TexChunk
*currentSection
= NULL
;
54 wxChar
*fakeCurrentSection
= NULL
;
56 static long BibLine
= 1;
58 void OutputCurrentSection(void)
60 if (fakeCurrentSection
)
61 TexOutput(fakeCurrentSection
);
62 else if (currentSection
)
63 TraverseChildrenFromChunk(currentSection
);
66 // Nasty but the way things are done now, necessary,
67 // in order to output a chunk properly to a string (macros and all).
68 void OutputCurrentSectionToString(wxChar
*buf
)
70 if (fakeCurrentSection
)
71 wxStrcpy(buf
, fakeCurrentSection
);
73 OutputChunkToString(currentSection
, buf
);
76 void OutputChunkToString(TexChunk
*chunk
, wxChar
*buf
)
78 FILE *tempfd
= wxFopen(_T("tmp.tmp"), _T("w"));
82 FILE *old1
= CurrentOutput1
;
83 FILE *old2
= CurrentOutput2
;
85 CurrentOutput1
= tempfd
;
86 CurrentOutput2
= NULL
;
88 TraverseChildrenFromChunk(chunk
);
90 CurrentOutput1
= old1
;
91 CurrentOutput2
= old2
;
95 // Read from file into string
96 tempfd
= wxFopen(_T("tmp.tmp"), _T("r"));
115 wxRemoveFile(_T("tmp.tmp"));
118 // Called by Tex2Any to simulate a section
119 void FakeCurrentSection(wxChar
*fakeSection
, bool addToContents
)
121 currentSection
= NULL
;
122 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
123 fakeCurrentSection
= copystring(fakeSection
);
125 if (DocumentStyle
== LATEX_ARTICLE
)
127 int mac
= ltSECTIONHEADING
;
129 mac
= ltSECTIONHEADINGSTAR
;
130 OnMacro(mac
, 0, true);
131 OnMacro(mac
, 0, false);
135 int mac
= ltCHAPTERHEADING
;
137 mac
= ltCHAPTERHEADINGSTAR
;
138 OnMacro(mac
, 0, true);
139 OnMacro(mac
, 0, false);
141 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
142 fakeCurrentSection
= NULL
;
145 // Look for \label macro, use this ref name if found or
146 // make up a topic name otherwise.
147 static long topicCounter
= 0;
149 void ResetTopicCounter(void)
154 static wxChar
*forceTopicName
= NULL
;
156 void ForceTopicName(const wxChar
*name
)
159 delete[] forceTopicName
;
161 forceTopicName
= copystring(name
);
163 forceTopicName
= NULL
;
166 wxChar
*FindTopicName(TexChunk
*chunk
)
169 return forceTopicName
;
171 wxChar
*topicName
= NULL
;
172 static wxChar topicBuf
[100];
174 if (chunk
&& (chunk
->type
== CHUNK_TYPE_MACRO
) &&
175 (chunk
->macroId
== ltLABEL
))
177 wxNode
*node
= chunk
->children
.GetFirst();
180 TexChunk
*child
= (TexChunk
*)node
->GetData();
181 if (child
->type
== CHUNK_TYPE_ARG
)
183 wxNode
*snode
= child
->children
.GetFirst();
186 TexChunk
*schunk
= (TexChunk
*)snode
->GetData();
187 if (schunk
->type
== CHUNK_TYPE_STRING
)
188 topicName
= schunk
->value
;
197 wxSnprintf(topicBuf
, sizeof(topicBuf
), _T("topic%ld"), topicCounter
);
204 * Simulate argument data, so we can 'drive' clients which implement
205 * certain basic formatting behaviour.
206 * Snag is that some save a TexChunk, so don't use yet...
210 void StartSimulateArgument(wxChar
*data
)
212 wxStrcpy(currentArgData
, data
);
216 void EndSimulateArgument(void)
222 * Parse and convert unit arguments to points
226 int ParseUnitArgument(wxChar
*unitArg
)
228 float conversionFactor
= 1.0;
229 float unitValue
= 0.0;
230 int len
= wxStrlen(unitArg
);
231 // Get rid of any accidentally embedded commands
232 for (int i
= 0; i
< len
; i
++)
233 if (unitArg
[i
] == '\\')
235 len
= wxStrlen(unitArg
);
237 if (unitArg
&& (len
> 0) && (isdigit(unitArg
[0]) || unitArg
[0] == '-'))
239 wxSscanf(unitArg
, _T("%f"), &unitValue
);
243 units
[0] = unitArg
[len
-2];
244 units
[1] = unitArg
[len
-1];
246 if (wxStrcmp(units
, _T("in")) == 0)
247 conversionFactor
= 72.0;
248 else if (wxStrcmp(units
, _T("cm")) == 0)
249 conversionFactor
= (float)72.0/(float)2.51;
250 else if (wxStrcmp(units
, _T("mm")) == 0)
251 conversionFactor
= (float)72.0/(float)25.1;
252 else if (wxStrcmp(units
, _T("pt")) == 0)
253 conversionFactor
= 1;
255 return (int)(unitValue
*conversionFactor
);
261 * Strip off any extension (dot something) from end of file,
262 * IF one exists. Inserts zero into buffer.
266 void StripExtension(wxChar
*buffer
)
268 int len
= wxStrlen(buffer
);
272 if (buffer
[i
] == '.')
286 void SetFontSizes(int pointSize
)
338 void AddTexRef(wxChar
*name
, wxChar
*file
, wxChar
*sectionName
,
339 int chapter
, int section
, int subsection
, int subsubsection
)
341 TexRef
*texRef
= (TexRef
*)TexReferences
.Get(name
);
342 if (texRef
) TexReferences
.Delete(name
);
349 wxStrcat(buf, sectionName);
356 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), chapter
);
363 wxStrcat(buf
, _T("."));
365 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), section
);
371 wxStrcat(buf
, _T("."));
372 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), subsection
);
378 wxStrcat(buf
, _T("."));
379 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), subsubsection
);
382 wxChar
*tmp
= ((wxStrlen(buf
) > 0) ? buf
: (wxChar
*)NULL
);
383 TexReferences
.Put(name
, new TexRef(name
, file
, tmp
, sectionName
));
386 void WriteTexReferences(wxChar
*filename
)
388 wxString name
= filename
;
391 if (!(wxFileExists(name
)?file
.Open(name
):file
.Create(name
)))
396 TexReferences
.BeginFind();
397 wxHashTable::Node
*node
= TexReferences
.Next();
401 TexRef
*ref
= (TexRef
*)node
->GetData();
402 wxString converter
= ref
->refLabel
;
403 converter
<< wxT(" ");
404 converter
<< (ref
->refFile
? ref
->refFile
: _T("??"));
405 converter
<< wxT(" ");
406 converter
<< (ref
->sectionName
? ref
->sectionName
: _T("??")) ;
407 converter
<< wxT(" ");
408 converter
<< (ref
->sectionNumber
? ref
->sectionNumber
: _T("??")) ;
409 file
.AddLine(converter
);
411 if (!ref
->sectionNumber
|| (wxStrcmp(ref
->sectionNumber
, _T("??")) == 0 && wxStrcmp(ref
->sectionName
, _T("??")) == 0))
414 wxSnprintf(buf
, sizeof(buf
), _T("Warning: reference %s not resolved."), ref
->refLabel
);
417 node
= TexReferences
.Next();
424 void ReadTexReferences(wxChar
*filename
)
426 wxString name
= filename
;
428 if (!wxFileExists(name
))
432 if (!file
.Open(name
))
436 for ( line
= file
.GetFirstLine(); !file
.Eof(); line
= file
.GetNextLine() )
438 wxString labelStr
= line
.BeforeFirst(wxT(' '));
439 line
= line
.AfterFirst(wxT(' '));
440 wxString fileStr
= line
.BeforeFirst(wxT(' '));
441 line
= line
.AfterFirst(wxT(' '));
442 wxString sectionNameStr
= line
.BeforeFirst(wxT(' '));
443 wxString sectionStr
= line
.AfterFirst(wxT(' '));
445 // gt - needed to trick the hash table "TexReferences" into deleting the key
446 // strings it creates in the Put() function, but not the item that is
447 // created here, as that is destroyed elsewhere. Without doing this, there
448 // were massive memory leaks
449 TexReferences
.DeleteContents(true);
456 sectionNameStr
.c_str()
459 TexReferences
.DeleteContents(false);
465 * Bibliography-handling code
469 void BibEatWhiteSpace(wxString
& line
)
471 while(!line
.empty() && (line
[0] == _T(' ') || line
[0] == _T('\t') || line
[0] == (wxChar
)EOF
))
475 line
= line
.substr(1);
478 // Ignore end-of-line comments
479 if (line
[0] == _T('%') || line
[0] == _T(';') || line
[0] == _T('#'))
481 line
= wxEmptyString
;
485 void BibEatWhiteSpace(wxSTD istream
& str
)
487 char ch
= (char)str
.peek();
489 while (!str
.eof() && (ch
== ' ' || ch
== '\t' || ch
== 13 || ch
== 10 || ch
== (char)EOF
))
494 if ((ch
== (char)EOF
) || str
.eof()) return;
495 ch
= (char)str
.peek();
498 // Ignore end-of-line comments
499 if (ch
== '%' || ch
== ';' || ch
== '#')
502 ch
= (char)str
.peek();
503 while (ch
!= 10 && ch
!= 13 && !str
.eof())
506 ch
= (char)str
.peek();
508 BibEatWhiteSpace(str
);
512 // Read word up to { or , or space
513 wxString
BibReadWord(wxString
& line
)
517 while (!line
.empty() &&
518 line
[0] != _T('\t') &&
519 line
[0] != _T(' ') &&
520 line
[0] != _T('{') &&
521 line
[0] != _T('(') &&
522 line
[0] != _T(',') &&
526 line
= line
.substr(1);
531 void BibReadWord(wxSTD istream
& istr
, wxChar
*buffer
)
535 char ch
= (char)istr
.peek();
536 while (!istr
.eof() && ch
!= ' ' && ch
!= '{' && ch
!= '(' && ch
!= 13 && ch
!= 10 && ch
!= '\t' &&
537 ch
!= ',' && ch
!= '=')
542 ch
= (char)istr
.peek();
547 // Read string (double-quoted or not) to end quote or EOL
548 wxString
BibReadToEOL(wxString
& line
)
551 return wxEmptyString
;
554 bool inQuotes
= false;
555 if (line
[0] == _T('"'))
557 line
= line
.substr(1);
560 // If in quotes, read white space too. If not,
561 // stop at white space or comment.
562 while (!line
.empty() && line
[0] != _T('"') &&
563 (inQuotes
|| ((line
[0] != _T(' ')) && (line
[0] != 9) &&
564 (line
[0] != _T(';')) && (line
[0] != _T('%')) && (line
[0] != _T('#')))))
567 line
= line
.substr(1);
570 line
= line
.substr(1);
575 void BibReadToEOL(wxSTD istream
& istr
, wxChar
*buffer
)
579 char ch
= (char)istr
.peek();
580 bool inQuotes
= false;
584 ch
= (char)istr
.peek();
587 // If in quotes, read white space too. If not,
588 // stop at white space or comment.
589 while (!istr
.eof() && ch
!= 13 && ch
!= 10 && ch
!= _T('"') &&
590 (inQuotes
|| ((ch
!= _T(' ')) && (ch
!= 9) &&
591 (ch
!= _T(';')) && (ch
!= _T('%')) && (ch
!= _T('#')))))
596 ch
= (char)istr
.peek();
603 // Read }-terminated value, taking nested braces into account.
604 wxString
BibReadValue(wxString
& line
,
605 bool ignoreBraces
= true,
606 bool quotesMayTerminate
= true)
610 bool stopping
= false;
612 if (line
.length() >= 4000)
615 wxSnprintf(buf
, sizeof(buf
), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine
);
616 wxLogError(buf
, "Tex2RTF Fatal Error");
617 return wxEmptyString
;
620 while (!line
.empty() && !stopping
)
623 line
= line
.substr(1);
637 else if (quotesMayTerminate
&& ch
== _T('"'))
645 if (!ignoreBraces
|| (ch
!= _T('{') && ch
!= _T('}')))
655 void BibReadValue(wxSTD istream
& istr
, wxChar
*buffer
, bool ignoreBraces
= true,
656 bool quotesMayTerminate
= true)
661 char ch
= (char)istr
.peek();
662 bool stopping
= false;
663 while (!istr
.eof() && !stopping
)
669 wxSnprintf(buf
, sizeof(buf
), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine
);
670 wxLogError(buf
, "Tex2RTF Fatal Error");
687 else if (quotesMayTerminate
&& ch
== '"')
694 if (!ignoreBraces
|| (ch
!= '{' && ch
!= '}'))
704 wxUnusedVar(stopping
);
707 bool ReadBib(wxChar
*filename
)
709 if (!wxFileExists(filename
))
712 wxString name
= filename
;
714 wxSTD ifstream
istr((char const *)name
.fn_str(), wxSTD
ios::in
);
715 if (istr
.bad()) return false;
719 OnInform(_T("Reading .bib file..."));
722 wxChar fieldValue
[4000];
723 wxChar recordType
[100];
724 wxChar recordKey
[100];
725 wxChar recordField
[100];
730 BibEatWhiteSpace(istr
);
734 wxSnprintf(buf
, sizeof(buf
), _T("Expected @: malformed bib file at line %ld (%s)"), BibLine
, filename
);
738 BibReadWord(istr
, recordType
);
739 BibEatWhiteSpace(istr
);
741 if (ch
!= '{' && ch
!= '(')
743 wxSnprintf(buf
, sizeof(buf
), _T("Expected { or ( after record type: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
747 BibEatWhiteSpace(istr
);
748 if (StringMatch(recordType
, _T("string"), false, true))
750 BibReadWord(istr
, recordType
);
751 BibEatWhiteSpace(istr
);
755 wxSnprintf(buf
, sizeof(buf
), _T("Expected = after string key: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
759 BibEatWhiteSpace(istr
);
761 if (ch
!= '"' && ch
!= '{')
763 wxSnprintf(buf
, sizeof(buf
), _T("Expected = after string key: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
767 BibReadValue(istr
, fieldValue
);
769 // Now put in hash table if necesary
770 if (!BibStringTable
.Get(recordType
))
771 BibStringTable
.Put(recordType
, (wxObject
*)copystring(fieldValue
));
773 // Read closing ) or }
774 BibEatWhiteSpace(istr
);
776 BibEatWhiteSpace(istr
);
780 BibReadWord(istr
, recordKey
);
782 BibEntry
*bibEntry
= new BibEntry
;
783 bibEntry
->key
= copystring(recordKey
);
784 bibEntry
->type
= copystring(recordType
);
786 bool moreRecords
= true;
787 while (moreRecords
&& !istr
.eof())
789 BibEatWhiteSpace(istr
);
791 if (ch
== '}' || ch
== ')')
797 BibEatWhiteSpace(istr
);
798 BibReadWord(istr
, recordField
);
799 BibEatWhiteSpace(istr
);
803 wxSnprintf(buf
, sizeof(buf
), _T("Expected = after field type: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
807 BibEatWhiteSpace(istr
);
809 if (ch
!= '{' && ch
!= '"')
812 BibReadWord(istr
, fieldValue
+1);
814 // If in the table of strings, replace with string from table.
815 wxChar
*s
= (wxChar
*)BibStringTable
.Get(fieldValue
);
818 wxStrcpy(fieldValue
, s
);
822 BibReadValue(istr
, fieldValue
, true, (ch
== _T('"') ? true : false));
824 // Now we can add a field
825 if (StringMatch(recordField
, _T("author"), false, true))
826 bibEntry
->author
= copystring(fieldValue
);
827 else if (StringMatch(recordField
, _T("key"), false, true))
829 else if (StringMatch(recordField
, _T("annotate"), false, true))
831 else if (StringMatch(recordField
, _T("abstract"), false, true))
833 else if (StringMatch(recordField
, _T("edition"), false, true))
835 else if (StringMatch(recordField
, _T("howpublished"), false, true))
837 else if (StringMatch(recordField
, _T("note"), false, true) || StringMatch(recordField
, _T("notes"), false, true))
839 else if (StringMatch(recordField
, _T("series"), false, true))
841 else if (StringMatch(recordField
, _T("type"), false, true))
843 else if (StringMatch(recordField
, _T("keywords"), false, true))
845 else if (StringMatch(recordField
, _T("editor"), false, true) || StringMatch(recordField
, _T("editors"), false, true))
846 bibEntry
->editor
= copystring(fieldValue
);
847 else if (StringMatch(recordField
, _T("title"), false, true))
848 bibEntry
->title
= copystring(fieldValue
);
849 else if (StringMatch(recordField
, _T("booktitle"), false, true))
850 bibEntry
->booktitle
= copystring(fieldValue
);
851 else if (StringMatch(recordField
, _T("journal"), false, true))
852 bibEntry
->journal
= copystring(fieldValue
);
853 else if (StringMatch(recordField
, _T("volume"), false, true))
854 bibEntry
->volume
= copystring(fieldValue
);
855 else if (StringMatch(recordField
, _T("number"), false, true))
856 bibEntry
->number
= copystring(fieldValue
);
857 else if (StringMatch(recordField
, _T("year"), false, true))
858 bibEntry
->year
= copystring(fieldValue
);
859 else if (StringMatch(recordField
, _T("month"), false, true))
860 bibEntry
->month
= copystring(fieldValue
);
861 else if (StringMatch(recordField
, _T("pages"), false, true))
862 bibEntry
->pages
= copystring(fieldValue
);
863 else if (StringMatch(recordField
, _T("publisher"), false, true))
864 bibEntry
->publisher
= copystring(fieldValue
);
865 else if (StringMatch(recordField
, _T("address"), false, true))
866 bibEntry
->address
= copystring(fieldValue
);
867 else if (StringMatch(recordField
, _T("institution"), false, true) || StringMatch(recordField
, _T("school"), false, true))
868 bibEntry
->institution
= copystring(fieldValue
);
869 else if (StringMatch(recordField
, _T("organization"), false, true) || StringMatch(recordField
, _T("organisation"), false, true))
870 bibEntry
->organization
= copystring(fieldValue
);
871 else if (StringMatch(recordField
, _T("comment"), false, true) || StringMatch(recordField
, _T("comments"), false, true))
872 bibEntry
->comment
= copystring(fieldValue
);
873 else if (StringMatch(recordField
, _T("annote"), false, true))
874 bibEntry
->comment
= copystring(fieldValue
);
875 else if (StringMatch(recordField
, _T("chapter"), false, true))
876 bibEntry
->chapter
= copystring(fieldValue
);
879 wxSnprintf(buf
, sizeof(buf
), _T("Unrecognised bib field type %s at line %ld (%s)"), recordField
, BibLine
, filename
);
884 BibList
.Append(recordKey
, bibEntry
);
885 BibEatWhiteSpace(istr
);
891 void OutputBibItem(TexRef
*ref
, BibEntry
*bib
)
895 OnMacro(ltNUMBEREDBIBITEM
, 2, true);
896 OnArgument(ltNUMBEREDBIBITEM
, 1, true);
897 TexOutput(ref
->sectionNumber
);
898 OnArgument(ltNUMBEREDBIBITEM
, 1, false);
899 OnArgument(ltNUMBEREDBIBITEM
, 2, true);
902 OnMacro(ltBF
, 1, true);
903 OnArgument(ltBF
, 1, true);
905 TexOutput(bib
->author
);
906 OnArgument(ltBF
, 1, false);
907 OnMacro(ltBF
, 1, false);
908 if (bib
->author
&& (wxStrlen(bib
->author
) > 0) && (bib
->author
[wxStrlen(bib
->author
) - 1] != '.'))
915 TexOutput(bib
->year
);
920 TexOutput(bib
->month
);
923 if (bib
->year
|| bib
->month
)
926 if (StringMatch(bib
->type
, _T("article"), false, true))
930 TexOutput(bib
->title
);
935 OnMacro(ltIT
, 1, true);
936 OnArgument(ltIT
, 1, true);
937 TexOutput(bib
->journal
);
938 OnArgument(ltIT
, 1, false);
939 OnMacro(ltIT
, 1, false);
944 OnMacro(ltBF
, 1, true);
945 OnArgument(ltBF
, 1, true);
946 TexOutput(bib
->volume
);
947 OnArgument(ltBF
, 1, false);
948 OnMacro(ltBF
, 1, false);
953 TexOutput(bib
->number
);
958 TexOutput(_T(", pages "));
959 TexOutput(bib
->pages
);
963 else if (StringMatch(bib
->type
, _T("book"), false, true) ||
964 StringMatch(bib
->type
, _T("unpublished"), false, true) ||
965 StringMatch(bib
->type
, _T("manual"), false, true) ||
966 StringMatch(bib
->type
, _T("phdthesis"), false, true) ||
967 StringMatch(bib
->type
, _T("mastersthesis"), false, true) ||
968 StringMatch(bib
->type
, _T("misc"), false, true) ||
969 StringMatch(bib
->type
, _T("techreport"), false, true) ||
970 StringMatch(bib
->type
, _T("booklet"), false, true))
972 if (bib
->title
|| bib
->booktitle
)
974 OnMacro(ltIT
, 1, true);
975 OnArgument(ltIT
, 1, true);
976 TexOutput(bib
->title
? bib
->title
: bib
->booktitle
);
978 OnArgument(ltIT
, 1, false);
979 OnMacro(ltIT
, 1, false);
981 if (StringMatch(bib
->type
, _T("phdthesis"), false, true))
982 TexOutput(_T("PhD thesis. "));
983 if (StringMatch(bib
->type
, _T("techreport"), false, true))
984 TexOutput(_T("Technical report. "));
987 TexOutput(_T("Ed. "));
988 TexOutput(bib
->editor
);
991 if (bib
->institution
)
993 TexOutput(bib
->institution
);
996 if (bib
->organization
)
998 TexOutput(bib
->organization
);
1003 TexOutput(bib
->publisher
);
1004 TexOutput(_T(". "));
1008 TexOutput(bib
->address
);
1009 TexOutput(_T(". "));
1012 else if (StringMatch(bib
->type
, _T("inbook"), false, true) ||
1013 StringMatch(bib
->type
, _T("inproceedings"), false, true) ||
1014 StringMatch(bib
->type
, _T("incollection"), false, true) ||
1015 StringMatch(bib
->type
, _T("conference"), false, true))
1019 TexOutput(bib
->title
);
1023 TexOutput(_T(", from "));
1024 OnMacro(ltIT
, 1, true);
1025 OnArgument(ltIT
, 1, true);
1026 TexOutput(bib
->booktitle
);
1028 OnArgument(ltIT
, 1, false);
1029 OnMacro(ltIT
, 1, false);
1033 TexOutput(_T(", ed. "));
1034 TexOutput(bib
->editor
);
1039 TexOutput(bib
->publisher
);
1043 if (bib
->publisher
) TexOutput(_T(", "));
1044 else TexOutput(_T(" "));
1045 TexOutput(bib
->address
);
1047 if (bib
->publisher
|| bib
->address
)
1053 OnMacro(ltBF
, 1, true);
1054 OnArgument(ltBF
, 1, true);
1055 TexOutput(bib
->volume
);
1056 OnArgument(ltBF
, 1, false);
1057 OnMacro(ltBF
, 1, false);
1064 TexOutput(bib
->number
);
1065 TexOutput(_T(")."));
1069 TexOutput(_T(" Number "));
1070 TexOutput(bib
->number
);
1076 TexOutput(_T(" Chap. "));
1077 TexOutput(bib
->chapter
);
1081 if (bib
->chapter
) TexOutput(_T(", pages "));
1082 else TexOutput(_T(" Pages "));
1083 TexOutput(bib
->pages
);
1087 OnArgument(ltNUMBEREDBIBITEM
, 2, false);
1088 OnMacro(ltNUMBEREDBIBITEM
, 2, false);
1091 void OutputBib(void)
1093 // Write the heading
1094 ForceTopicName(_T("bibliography"));
1095 FakeCurrentSection(ReferencesNameString
);
1096 ForceTopicName(NULL
);
1098 OnMacro(ltPAR
, 0, true);
1099 OnMacro(ltPAR
, 0, false);
1101 if ((convertMode
== TEX_RTF
) && !winHelp
)
1103 OnMacro(ltPAR
, 0, true);
1104 OnMacro(ltPAR
, 0, false);
1107 wxStringListNode
*node
= CitationList
.GetFirst();
1110 wxChar
*citeKey
= (wxChar
*)node
->GetData();
1111 // wxNode *texNode = TexReferences.Find(citeKey);
1112 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
1113 wxNode
*bibNode
= BibList
.Find(citeKey
);
1116 BibEntry
*entry
= (BibEntry
*)bibNode
->GetData();
1117 OutputBibItem(ref
, entry
);
1119 node
= node
->GetNext();
1123 static int citeCount
= 1;
1125 void ResolveBibReferences(void)
1127 if (CitationList
.GetCount() > 0)
1128 OnInform(_T("Resolving bibliographic references..."));
1132 wxStringListNode
*node
= CitationList
.GetFirst();
1136 wxChar
*citeKey
= (wxChar
*)node
->GetData();
1137 // wxNode *texNode = TexReferences.Find(citeKey);
1138 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
1139 wxNode
*bibNode
= BibList
.Find(citeKey
);
1143 //BibEntry *entry = (BibEntry *)bibNode->GetData();
1144 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
1145 wxSnprintf(buf
, sizeof(buf
), _T("[%d]"), citeCount
);
1146 ref
->sectionNumber
= copystring(buf
);
1151 wxSnprintf(buf
, sizeof(buf
), _T("Warning: bib ref %s not resolved."), citeKey
);
1154 node
= node
->GetNext();
1158 // Remember we need to resolve this citation
1159 void AddCitation(wxChar
*citeKey
)
1161 if (!CitationList
.Member(citeKey
))
1162 CitationList
.Add(citeKey
);
1164 if (!TexReferences
.Get(citeKey
))
1166 TexReferences
.Put(citeKey
, new TexRef(citeKey
, _T("??"), NULL
));
1170 TexRef
*FindReference(wxChar
*key
)
1172 return (TexRef
*)TexReferences
.Get(key
);
1176 * Custom macro stuff
1180 bool StringTobool(const wxString
& val
)
1185 if (up
.IsSameAs(_T("YES")) ||
1186 up
.IsSameAs(_T("TRUE")) ||
1187 up
.IsSameAs(_T("ON")) ||
1188 up
.IsSameAs(_T("OK")) |
1189 up
.IsSameAs(_T("1")))
1195 void RegisterIntSetting (const wxString
& s
, int *number
)
1205 // Define a variable value from the .ini file
1206 wxChar
*RegisterSetting(const wxString
& settingName
, const wxString
& settingValue
, bool interactive
)
1208 wxString
settingValueStr( settingValue
);
1210 static wxChar errorCode
[100];
1211 wxStrcpy(errorCode
, _T("OK"));
1212 if (StringMatch(settingName
, _T("chapterName"), false, true))
1214 delete[] ChapterNameString
;
1215 ChapterNameString
= copystring(settingValue
);
1217 else if (StringMatch(settingName
, _T("sectionName"), false, true))
1219 delete[] SectionNameString
;
1220 SectionNameString
= copystring(settingValue
);
1222 else if (StringMatch(settingName
, _T("subsectionName"), false, true))
1224 delete[] SubsectionNameString
;
1225 SubsectionNameString
= copystring(settingValue
);
1227 else if (StringMatch(settingName
, _T("subsubsectionName"), false, true))
1229 delete[] SubsubsectionNameString
;
1230 SubsubsectionNameString
= copystring(settingValue
);
1232 else if (StringMatch(settingName
, _T("indexName"), false, true))
1234 delete[] IndexNameString
;
1235 IndexNameString
= copystring(settingValue
);
1237 else if (StringMatch(settingName
, _T("contentsName"), false, true))
1239 delete[] ContentsNameString
;
1240 ContentsNameString
= copystring(settingValue
);
1242 else if (StringMatch(settingName
, _T("glossaryName"), false, true))
1244 delete[] GlossaryNameString
;
1245 GlossaryNameString
= copystring(settingValue
);
1247 else if (StringMatch(settingName
, _T("referencesName"), false, true))
1249 delete[] ReferencesNameString
;
1250 ReferencesNameString
= copystring(settingValue
);
1252 else if (StringMatch(settingName
, _T("tablesName"), false, true))
1254 delete[] TablesNameString
;
1255 TablesNameString
= copystring(settingValue
);
1257 else if (StringMatch(settingName
, _T("figuresName"), false, true))
1259 delete[] FiguresNameString
;
1260 FiguresNameString
= copystring(settingValue
);
1262 else if (StringMatch(settingName
, _T("tableName"), false, true))
1264 delete[] TableNameString
;
1265 TableNameString
= copystring(settingValue
);
1267 else if (StringMatch(settingName
, _T("figureName"), false, true))
1269 delete[] FigureNameString
;
1270 FigureNameString
= copystring(settingValue
);
1272 else if (StringMatch(settingName
, _T("abstractName"), false, true))
1274 delete[] AbstractNameString
;
1275 AbstractNameString
= copystring(settingValue
);
1277 else if (StringMatch(settingName
, _T("chapterFontSize"), false, true))
1278 RegisterIntSetting(settingValueStr
, &chapterFont
);
1279 else if (StringMatch(settingName
, _T("sectionFontSize"), false, true))
1280 RegisterIntSetting(settingValueStr
, §ionFont
);
1281 else if (StringMatch(settingName
, _T("subsectionFontSize"), false, true))
1282 RegisterIntSetting(settingValueStr
, &subsectionFont
);
1283 else if (StringMatch(settingName
, _T("titleFontSize"), false, true))
1284 RegisterIntSetting(settingValueStr
, &titleFont
);
1285 else if (StringMatch(settingName
, _T("authorFontSize"), false, true))
1286 RegisterIntSetting(settingValueStr
, &authorFont
);
1287 else if (StringMatch(settingName
, _T("ignoreInput"), false, true))
1288 IgnorableInputFiles
.Add(wxFileNameFromPath(settingValue
));
1289 else if (StringMatch(settingName
, _T("mirrorMargins"), false, true))
1290 mirrorMargins
= StringTobool(settingValue
);
1291 else if (StringMatch(settingName
, _T("runTwice"), false, true))
1292 runTwice
= StringTobool(settingValue
);
1293 else if (StringMatch(settingName
, _T("isInteractive"), false, true))
1294 isInteractive
= StringTobool(settingValue
);
1295 else if (StringMatch(settingName
, _T("headerRule"), false, true))
1296 headerRule
= StringTobool(settingValue
);
1297 else if (StringMatch(settingName
, _T("footerRule"), false, true))
1298 footerRule
= StringTobool(settingValue
);
1299 else if (StringMatch(settingName
, _T("combineSubSections"), false, true))
1300 combineSubSections
= StringTobool(settingValue
);
1301 else if (StringMatch(settingName
, _T("listLabelIndent"), false, true))
1302 RegisterIntSetting(settingValueStr
, &labelIndentTab
);
1303 else if (StringMatch(settingName
, _T("listItemIndent"), false, true))
1304 RegisterIntSetting(settingValueStr
, &itemIndentTab
);
1305 else if (StringMatch(settingName
, _T("useUpButton"), false, true))
1306 useUpButton
= StringTobool(settingValue
);
1307 else if (StringMatch(settingName
, _T("useHeadingStyles"), false, true))
1308 useHeadingStyles
= StringTobool(settingValue
);
1309 else if (StringMatch(settingName
, _T("useWord"), false, true))
1310 useWord
= StringTobool(settingValue
);
1311 else if (StringMatch(settingName
, _T("contentsDepth"), false, true))
1312 RegisterIntSetting(settingValueStr
, &contentsDepth
);
1313 else if (StringMatch(settingName
, _T("generateHPJ"), false, true))
1314 generateHPJ
= StringTobool(settingValue
);
1315 else if (StringMatch(settingName
, _T("truncateFilenames"), false, true))
1316 truncateFilenames
= StringTobool(settingValue
);
1317 else if (StringMatch(settingName
, _T("winHelpVersion"), false, true))
1318 RegisterIntSetting(settingValueStr
, &winHelpVersion
);
1319 else if (StringMatch(settingName
, _T("winHelpContents"), false, true))
1320 winHelpContents
= StringTobool(settingValue
);
1321 else if (StringMatch(settingName
, _T("htmlIndex"), false, true))
1322 htmlIndex
= StringTobool(settingValue
);
1323 else if (StringMatch(settingName
, _T("htmlWorkshopFiles"), false, true))
1324 htmlWorkshopFiles
= StringTobool(settingValue
);
1325 else if (StringMatch(settingName
, _T("htmlFrameContents"), false, true))
1326 htmlFrameContents
= StringTobool(settingValue
);
1327 else if (StringMatch(settingName
, _T("htmlStylesheet"), false, true))
1330 delete[] htmlStylesheet
;
1331 htmlStylesheet
= copystring(settingValue
);
1333 else if (StringMatch(settingName
, _T("upperCaseNames"), false, true))
1334 upperCaseNames
= StringTobool(settingValue
);
1335 else if (StringMatch(settingName
, _T("ignoreBadRefs"), false, true))
1336 ignoreBadRefs
= StringTobool(settingValue
);
1337 else if (StringMatch(settingName
, _T("htmlFaceName"), false, true))
1339 delete[] htmlFaceName
;
1340 htmlFaceName
= copystring(settingValue
);
1342 else if (StringMatch(settingName
, _T("winHelpTitle"), false, true))
1345 delete[] winHelpTitle
;
1346 winHelpTitle
= copystring(settingValue
);
1348 else if (StringMatch(settingName
, _T("indexSubsections"), false, true))
1349 indexSubsections
= StringTobool(settingValue
);
1350 else if (StringMatch(settingName
, _T("compatibility"), false, true))
1351 compatibilityMode
= StringTobool(settingValue
);
1352 else if (StringMatch(settingName
, _T("defaultColumnWidth"), false, true))
1354 RegisterIntSetting(settingValueStr
, &defaultTableColumnWidth
);
1355 defaultTableColumnWidth
= 20*defaultTableColumnWidth
;
1357 else if (StringMatch(settingName
, _T("bitmapMethod"), false, true))
1359 if ((wxStrcmp(settingValue
, _T("includepicture")) != 0) && (wxStrcmp(settingValue
, _T("hex")) != 0) &&
1360 (wxStrcmp(settingValue
, _T("import")) != 0))
1363 OnError(_T("Unknown bitmapMethod"));
1364 wxStrcpy(errorCode
, _T("Unknown bitmapMethod"));
1368 delete[] bitmapMethod
;
1369 bitmapMethod
= copystring(settingValue
);
1372 else if (StringMatch(settingName
, _T("htmlBrowseButtons"), false, true))
1374 if (wxStrcmp(settingValue
, _T("none")) == 0)
1375 htmlBrowseButtons
= HTML_BUTTONS_NONE
;
1376 else if (wxStrcmp(settingValue
, _T("bitmap")) == 0)
1377 htmlBrowseButtons
= HTML_BUTTONS_BITMAP
;
1378 else if (wxStrcmp(settingValue
, _T("text")) == 0)
1379 htmlBrowseButtons
= HTML_BUTTONS_TEXT
;
1383 OnInform(_T("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text."));
1384 wxStrcpy(errorCode
, _T("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text."));
1387 else if (StringMatch(settingName
, _T("backgroundImage"), false, true))
1389 backgroundImageString
= copystring(settingValue
);
1391 else if (StringMatch(settingName
, _T("backgroundColour"), false, true))
1393 delete[] backgroundColourString
;
1394 backgroundColourString
= copystring(settingValue
);
1396 else if (StringMatch(settingName
, _T("textColour"), false, true))
1398 textColourString
= copystring(settingValue
);
1400 else if (StringMatch(settingName
, _T("linkColour"), false, true))
1402 linkColourString
= copystring(settingValue
);
1404 else if (StringMatch(settingName
, _T("followedLinkColour"), false, true))
1406 followedLinkColourString
= copystring(settingValue
);
1408 else if (StringMatch(settingName
, _T("conversionMode"), false, true))
1410 if (StringMatch(settingValue
, _T("RTF"), false, true))
1412 winHelp
= false; convertMode
= TEX_RTF
;
1414 else if (StringMatch(settingValue
, _T("WinHelp"), false, true))
1416 winHelp
= true; convertMode
= TEX_RTF
;
1418 else if (StringMatch(settingValue
, _T("XLP"), false, true) ||
1419 StringMatch(settingValue
, _T("wxHelp"), false, true))
1421 convertMode
= TEX_XLP
;
1423 else if (StringMatch(settingValue
, _T("HTML"), false, true))
1425 convertMode
= TEX_HTML
;
1430 OnInform(_T("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML."));
1431 wxStrcpy(errorCode
, _T("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML."));
1434 else if (StringMatch(settingName
, _T("documentFontSize"), false, true))
1437 RegisterIntSetting(settingValueStr
, &n
);
1438 if (n
== 10 || n
== 11 || n
== 12)
1443 wxSnprintf(buf
, sizeof(buf
), _T("Initialisation file error: nonstandard document font size %d."), n
);
1446 wxStrcpy(errorCode
, buf
);
1452 wxSnprintf(buf
, sizeof(buf
), _T("Initialisation file error: unrecognised setting %s."), settingName
.c_str());
1455 wxStrcpy(errorCode
, buf
);
1460 bool ReadCustomMacros(const wxString
& filename
)
1462 if (!wxFileExists(filename
))
1465 wxFileInputStream
input( filename
);
1466 if(!input
.Ok()) return false;
1467 wxTextInputStream
ini( input
);
1469 CustomMacroList
.Clear();
1471 while (!input
.Eof())
1473 wxString line
= ini
.ReadLine();
1474 BibEatWhiteSpace(line
);
1475 if (line
.empty()) continue;
1477 if (line
[0] != _T('\\')) // Not a macro definition, so must be NAME=VALUE
1479 wxString settingName
= BibReadWord(line
);
1480 BibEatWhiteSpace(line
);
1481 if (line
.empty() || line
[0] != _T('='))
1483 OnError(_T("Expected = following name: malformed tex2rtf.ini file."));
1488 line
= line
.substr(1);
1489 BibEatWhiteSpace(line
);
1490 wxString settingValue
= BibReadToEOL(line
);
1491 RegisterSetting(settingName
, settingValue
);
1496 line
= line
.substr(1);
1497 wxString macroName
= BibReadWord(line
);
1498 BibEatWhiteSpace(line
);
1499 if (line
[0] != _T('['))
1501 OnError(_T("Expected [ followed by number of arguments: malformed tex2rtf.ini file."));
1504 line
= line
.substr(1);
1505 wxString noAargStr
= line
.BeforeFirst(_T(']'));
1506 line
= line
.AfterFirst(_T(']'));
1508 if (!noAargStr
.ToLong(&noArgs
) || line
.empty())
1510 OnError(_T("Expected ] following number of arguments: malformed tex2rtf.ini file."));
1513 BibEatWhiteSpace(line
);
1514 if (line
[0] != _T('{'))
1516 OnError(_T("Expected { followed by macro body: malformed tex2rtf.ini file."));
1520 CustomMacro
*macro
= new CustomMacro(macroName
.c_str(), noArgs
, NULL
);
1521 wxString macroBody
= BibReadValue(line
, false, false); // Don't ignore extra braces
1522 if (!macroBody
.empty())
1523 macro
->macroBody
= copystring(macroBody
.c_str());
1525 BibEatWhiteSpace(line
);
1526 CustomMacroList
.Append(macroName
.c_str(), macro
);
1527 AddMacroDef(ltCUSTOM_MACRO
, macroName
.c_str(), noArgs
);
1532 wxSnprintf(mbuf
, sizeof(mbuf
), _T("Read initialization file %s."), filename
.c_str());
1537 CustomMacro
*FindCustomMacro(wxChar
*name
)
1539 wxNode
*node
= CustomMacroList
.Find(name
);
1542 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
1548 // Display custom macros
1549 void ShowCustomMacros(void)
1551 wxNode
*node
= CustomMacroList
.GetFirst();
1554 OnInform(_T("No custom macros loaded.\n"));
1561 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
1562 wxSnprintf(buf
, sizeof(buf
), _T("\\%s[%d]\n {%s}"), macro
->macroName
, macro
->noArgs
,
1563 macro
->macroBody
? macro
->macroBody
: _T(""));
1565 node
= node
->GetNext();
1569 // Parse a string into several comma-separated fields
1570 wxChar
*ParseMultifieldString(wxChar
*allFields
, int *pos
)
1572 static wxChar buffer
[300];
1574 int fieldIndex
= *pos
;
1575 int len
= wxStrlen(allFields
);
1577 bool keepGoing
= true;
1578 while ((fieldIndex
<= len
) && keepGoing
)
1580 if (allFields
[fieldIndex
] == _T(' '))
1585 else if (allFields
[fieldIndex
] == _T(','))
1587 *pos
= fieldIndex
+ 1;
1590 else if (allFields
[fieldIndex
] == 0)
1592 *pos
= fieldIndex
+ 1;
1597 buffer
[i
] = allFields
[fieldIndex
];
1603 if (oldPos
== (*pos
))
1617 ColourTableEntry::ColourTableEntry(const wxChar
*theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1619 name
= copystring(theName
);
1625 ColourTableEntry::~ColourTableEntry(void)
1630 void AddColour(const wxChar
*theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1632 wxNode
*node
= ColourTable
.Find(theName
);
1635 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->GetData();
1636 if (entry
->red
== r
|| entry
->green
== g
|| entry
->blue
== b
)
1644 ColourTableEntry
*entry
= new ColourTableEntry(theName
, r
, g
, b
);
1645 ColourTable
.Append(theName
, entry
);
1648 int FindColourPosition(wxChar
*theName
)
1651 wxNode
*node
= ColourTable
.GetFirst();
1654 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->GetData();
1655 if (wxStrcmp(theName
, entry
->name
) == 0)
1658 node
= node
->GetNext();
1663 // Converts e.g. "red" -> "#FF0000"
1664 extern void DecToHex(int, wxChar
*);
1665 bool FindColourHTMLString(wxChar
*theName
, wxChar
*buf
)
1667 wxNode
*node
= ColourTable
.GetFirst();
1670 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->GetData();
1671 if (wxStrcmp(theName
, entry
->name
) == 0)
1673 wxStrcpy(buf
, _T("#"));
1676 DecToHex(entry
->red
, buf2
);
1677 wxStrcat(buf
, buf2
);
1678 DecToHex(entry
->green
, buf2
);
1679 wxStrcat(buf
, buf2
);
1680 DecToHex(entry
->blue
, buf2
);
1681 wxStrcat(buf
, buf2
);
1685 node
= node
->GetNext();
1691 void InitialiseColourTable(void)
1693 // \\red0\\green0\\blue0;
1694 AddColour(_T("black"), 0,0,0);
1696 // \\red0\\green0\\blue255;\\red0\\green255\\blue255;\n");
1697 AddColour(_T("cyan"), 0,255,255);
1699 // \\red0\\green255\\blue0;
1700 AddColour(_T("green"), 0,255,0);
1702 // \\red255\\green0\\blue255;
1703 AddColour(_T("magenta"), 255,0,255);
1705 // \\red255\\green0\\blue0;
1706 AddColour(_T("red"), 255,0,0);
1708 // \\red255\\green255\\blue0;
1709 AddColour(_T("yellow"), 255,255,0);
1711 // \\red255\\green255\\blue255;}");
1712 AddColour(_T("white"), 255,255,255);
1716 * The purpose of this is to reduce the number of times wxYield is
1717 * called, since under Windows this can slow things down.
1720 void Tex2RTFYield(bool force
)
1723 static int yieldCount
= 0;
1730 if (yieldCount
== 0)
1742 // In both RTF generation and HTML generation for wxHelp version 2,
1743 // we need to associate \indexed keywords with the current filename/topics.
1745 // Hash table for lists of keywords for topics (WinHelp).
1746 wxHashTable
TopicTable(wxKEY_STRING
);
1747 void AddKeyWordForTopic(wxChar
*topic
, wxChar
*entry
, wxChar
*filename
)
1749 TexTopic
*texTopic
= (TexTopic
*)TopicTable
.Get(topic
);
1752 texTopic
= new TexTopic(filename
);
1753 texTopic
->keywords
= new wxStringList
;
1754 TopicTable
.Put(topic
, texTopic
);
1757 if (!texTopic
->keywords
->Member(entry
))
1758 texTopic
->keywords
->Add(entry
);
1761 void ClearKeyWordTable(void)
1763 TopicTable
.BeginFind();
1764 wxHashTable::Node
*node
= TopicTable
.Next();
1767 TexTopic
*texTopic
= (TexTopic
*)node
->GetData();
1769 node
= TopicTable
.Next();
1776 * TexTopic structure
1779 TexTopic::TexTopic(wxChar
*f
)
1782 filename
= copystring(f
);
1785 hasChildren
= false;
1789 TexTopic::~TexTopic(void)
1797 // Convert case, according to upperCaseNames setting.
1798 wxChar
*ConvertCase(wxChar
*s
)
1800 static wxChar buf
[256];
1801 int len
= wxStrlen(s
);
1804 for (i
= 0; i
< len
; i
++)
1805 buf
[i
] = (wxChar
)wxToupper(s
[i
]);
1807 for (i
= 0; i
< len
; i
++)
1808 buf
[i
] = (wxChar
)wxTolower(s
[i
]);
1813 // if substring is true, search for str1 in str2
1814 bool StringMatch(const wxChar
*str1
, const wxChar
*str2
, bool subString
,
1819 wxString
Sstr1(str1
);
1820 wxString
Sstr2(str2
);
1826 return Sstr2
.Index(Sstr1
) != (size_t)wxNOT_FOUND
;
1829 return exact
? wxString(str2
).Cmp(str1
) == 0 :
1830 wxString(str2
).CmpNoCase(str1
) == 0;