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"
43 #if !WXWIN_COMPATIBILITY_2_4
44 static inline wxChar
* copystring(const wxChar
* s
)
45 { return wxStrcpy(new wxChar
[wxStrlen(s
) + 1], s
); }
48 wxHashTable
TexReferences(wxKEY_STRING
);
49 wxList
BibList(wxKEY_STRING
);
50 wxStringList CitationList
;
51 wxList
ColourTable(wxKEY_STRING
);
52 wxHashTable
BibStringTable(wxKEY_STRING
);
53 wxList
CustomMacroList(wxKEY_STRING
);
54 TexChunk
*currentSection
= NULL
;
55 wxChar
*fakeCurrentSection
= NULL
;
57 static long BibLine
= 1;
59 void OutputCurrentSection(void)
61 if (fakeCurrentSection
)
62 TexOutput(fakeCurrentSection
);
63 else if (currentSection
)
64 TraverseChildrenFromChunk(currentSection
);
67 // Nasty but the way things are done now, necessary,
68 // in order to output a chunk properly to a string (macros and all).
69 void OutputCurrentSectionToString(wxChar
*buf
)
71 if (fakeCurrentSection
)
72 wxStrcpy(buf
, fakeCurrentSection
);
74 OutputChunkToString(currentSection
, buf
);
77 void OutputChunkToString(TexChunk
*chunk
, wxChar
*buf
)
79 FILE *tempfd
= wxFopen(_T("tmp.tmp"), _T("w"));
83 FILE *old1
= CurrentOutput1
;
84 FILE *old2
= CurrentOutput2
;
86 CurrentOutput1
= tempfd
;
87 CurrentOutput2
= NULL
;
89 TraverseChildrenFromChunk(chunk
);
91 CurrentOutput1
= old1
;
92 CurrentOutput2
= old2
;
96 // Read from file into string
97 tempfd
= wxFopen(_T("tmp.tmp"), _T("r"));
116 wxRemoveFile(_T("tmp.tmp"));
119 // Called by Tex2Any to simulate a section
120 void FakeCurrentSection(wxChar
*fakeSection
, bool addToContents
)
122 currentSection
= NULL
;
123 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
124 fakeCurrentSection
= copystring(fakeSection
);
126 if (DocumentStyle
== LATEX_ARTICLE
)
128 int mac
= ltSECTIONHEADING
;
130 mac
= ltSECTIONHEADINGSTAR
;
131 OnMacro(mac
, 0, true);
132 OnMacro(mac
, 0, false);
136 int mac
= ltCHAPTERHEADING
;
138 mac
= ltCHAPTERHEADINGSTAR
;
139 OnMacro(mac
, 0, true);
140 OnMacro(mac
, 0, false);
142 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
143 fakeCurrentSection
= NULL
;
146 // Look for \label macro, use this ref name if found or
147 // make up a topic name otherwise.
148 static long topicCounter
= 0;
150 void ResetTopicCounter(void)
155 static wxChar
*forceTopicName
= NULL
;
157 void ForceTopicName(const wxChar
*name
)
160 delete[] forceTopicName
;
162 forceTopicName
= copystring(name
);
164 forceTopicName
= NULL
;
167 wxChar
*FindTopicName(TexChunk
*chunk
)
170 return forceTopicName
;
172 wxChar
*topicName
= NULL
;
173 static wxChar topicBuf
[100];
175 if (chunk
&& (chunk
->type
== CHUNK_TYPE_MACRO
) &&
176 (chunk
->macroId
== ltLABEL
))
178 wxNode
*node
= chunk
->children
.GetFirst();
181 TexChunk
*child
= (TexChunk
*)node
->GetData();
182 if (child
->type
== CHUNK_TYPE_ARG
)
184 wxNode
*snode
= child
->children
.GetFirst();
187 TexChunk
*schunk
= (TexChunk
*)snode
->GetData();
188 if (schunk
->type
== CHUNK_TYPE_STRING
)
189 topicName
= schunk
->value
;
198 wxSnprintf(topicBuf
, sizeof(topicBuf
), _T("topic%ld"), topicCounter
);
205 * Simulate argument data, so we can 'drive' clients which implement
206 * certain basic formatting behaviour.
207 * Snag is that some save a TexChunk, so don't use yet...
211 void StartSimulateArgument(wxChar
*data
)
213 wxStrcpy(currentArgData
, data
);
217 void EndSimulateArgument(void)
223 * Parse and convert unit arguments to points
227 int ParseUnitArgument(wxChar
*unitArg
)
229 float conversionFactor
= 1.0;
230 float unitValue
= 0.0;
231 int len
= wxStrlen(unitArg
);
232 // Get rid of any accidentally embedded commands
233 for (int i
= 0; i
< len
; i
++)
234 if (unitArg
[i
] == '\\')
236 len
= wxStrlen(unitArg
);
238 if (unitArg
&& (len
> 0) && (isdigit(unitArg
[0]) || unitArg
[0] == '-'))
240 wxSscanf(unitArg
, _T("%f"), &unitValue
);
244 units
[0] = unitArg
[len
-2];
245 units
[1] = unitArg
[len
-1];
247 if (wxStrcmp(units
, _T("in")) == 0)
248 conversionFactor
= 72.0;
249 else if (wxStrcmp(units
, _T("cm")) == 0)
250 conversionFactor
= (float)72.0/(float)2.51;
251 else if (wxStrcmp(units
, _T("mm")) == 0)
252 conversionFactor
= (float)72.0/(float)25.1;
253 else if (wxStrcmp(units
, _T("pt")) == 0)
254 conversionFactor
= 1;
256 return (int)(unitValue
*conversionFactor
);
262 * Strip off any extension (dot something) from end of file,
263 * IF one exists. Inserts zero into buffer.
267 void StripExtension(wxChar
*buffer
)
269 int len
= wxStrlen(buffer
);
273 if (buffer
[i
] == '.')
287 void SetFontSizes(int pointSize
)
339 void AddTexRef(wxChar
*name
, wxChar
*file
, wxChar
*sectionName
,
340 int chapter
, int section
, int subsection
, int subsubsection
)
342 TexRef
*texRef
= (TexRef
*)TexReferences
.Get(name
);
343 if (texRef
) TexReferences
.Delete(name
);
350 wxStrcat(buf, sectionName);
357 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), chapter
);
364 wxStrcat(buf
, _T("."));
366 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), section
);
372 wxStrcat(buf
, _T("."));
373 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), subsection
);
379 wxStrcat(buf
, _T("."));
380 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), subsubsection
);
383 wxChar
*tmp
= ((wxStrlen(buf
) > 0) ? buf
: (wxChar
*)NULL
);
384 TexReferences
.Put(name
, new TexRef(name
, file
, tmp
, sectionName
));
387 void WriteTexReferences(wxChar
*filename
)
390 wxString name
= filename
;
391 wxSTD ofstream
ostr((char const *)name
.fn_str());
392 if (ostr
.bad()) return;
394 TexReferences
.BeginFind();
395 wxHashTable::Node
*node
= TexReferences
.Next();
399 TexRef
*ref
= (TexRef
*)node
->GetData();
400 converter
= ref
->refLabel
;
401 ostr
<< converter
.mb_str();
403 converter
= (ref
->refFile
? ref
->refFile
: _T("??"));
404 ostr
<< converter
.mb_str();
406 converter
= (ref
->sectionName
? ref
->sectionName
: _T("??")) ;
407 ostr
<< converter
.mb_str();
409 converter
= (ref
->sectionNumber
? ref
->sectionNumber
: _T("??")) ;
410 ostr
<< converter
.mb_str();
412 if (!ref
->sectionNumber
|| (wxStrcmp(ref
->sectionNumber
, _T("??")) == 0 && wxStrcmp(ref
->sectionName
, _T("??")) == 0))
415 wxSnprintf(buf
, sizeof(buf
), _T("Warning: reference %s not resolved."), ref
->refLabel
);
418 node
= TexReferences
.Next();
422 void ReadTexReferences(wxChar
*filename
)
424 if (!wxFileExists(filename
))
427 wxString name
= filename
;
428 wxSTD ifstream
istr((char const *)name
.fn_str(), wxSTD
ios::in
);
430 if (istr
.bad()) return;
435 char sectionName
[100];
445 istr
.get(ch
); // Read past space
448 while (ch
!= '\n' && !istr
.eof())
456 wxString label_string
= wxString::FromAscii(label
);
457 wxString file_string
= wxString::FromAscii(file
);
458 wxString sectionName_string
= wxString::FromAscii(sectionName
);
459 wxString section_string
= wxString::FromAscii(section
);
461 // gt - needed to trick the hash table "TexReferences" into deleting the key
462 // strings it creates in the Put() function, but not the item that is
463 // created here, as that is destroyed elsewhere. Without doing this, there
464 // were massive memory leaks
465 TexReferences
.DeleteContents(true);
467 label_string
.c_str(),
469 label_string
.c_str(),
471 section_string
.c_str(),
472 sectionName_string
.c_str()
475 TexReferences
.DeleteContents(false);
482 * Bibliography-handling code
486 void BibEatWhiteSpace(wxString
& line
)
488 while(!line
.empty() && (line
[0] == _T(' ') || line
[0] == _T('\t') || line
[0] == (wxChar
)EOF
))
492 line
= line
.substr(1);
495 // Ignore end-of-line comments
496 if (line
[0] == _T('%') || line
[0] == _T(';') || line
[0] == _T('#'))
498 line
= wxEmptyString
;
502 void BibEatWhiteSpace(wxSTD istream
& str
)
504 char ch
= (char)str
.peek();
506 while (!str
.eof() && (ch
== ' ' || ch
== '\t' || ch
== 13 || ch
== 10 || ch
== (char)EOF
))
511 if ((ch
== (char)EOF
) || str
.eof()) return;
512 ch
= (char)str
.peek();
515 // Ignore end-of-line comments
516 if (ch
== '%' || ch
== ';' || ch
== '#')
519 ch
= (char)str
.peek();
520 while (ch
!= 10 && ch
!= 13 && !str
.eof())
523 ch
= (char)str
.peek();
525 BibEatWhiteSpace(str
);
529 // Read word up to { or , or space
530 wxString
BibReadWord(wxString
& line
)
534 while (!line
.empty() &&
535 line
[0] != _T('\t') &&
536 line
[0] != _T(' ') &&
537 line
[0] != _T('{') &&
538 line
[0] != _T('(') &&
539 line
[0] != _T(',') &&
543 line
= line
.substr(1);
548 void BibReadWord(wxSTD istream
& istr
, wxChar
*buffer
)
552 char ch
= (char)istr
.peek();
553 while (!istr
.eof() && ch
!= ' ' && ch
!= '{' && ch
!= '(' && ch
!= 13 && ch
!= 10 && ch
!= '\t' &&
554 ch
!= ',' && ch
!= '=')
559 ch
= (char)istr
.peek();
564 // Read string (double-quoted or not) to end quote or EOL
565 wxString
BibReadToEOL(wxString
& line
)
568 return wxEmptyString
;
571 bool inQuotes
= false;
572 if (line
[0] == _T('"'))
574 line
= line
.substr(1);
577 // If in quotes, read white space too. If not,
578 // stop at white space or comment.
579 while (!line
.empty() && line
[0] != _T('"') &&
580 (inQuotes
|| ((line
[0] != _T(' ')) && (line
[0] != 9) &&
581 (line
[0] != _T(';')) && (line
[0] != _T('%')) && (line
[0] != _T('#')))))
584 line
= line
.substr(1);
587 line
= line
.substr(1);
592 void BibReadToEOL(wxSTD istream
& istr
, wxChar
*buffer
)
596 char ch
= (char)istr
.peek();
597 bool inQuotes
= false;
601 ch
= (char)istr
.peek();
604 // If in quotes, read white space too. If not,
605 // stop at white space or comment.
606 while (!istr
.eof() && ch
!= 13 && ch
!= 10 && ch
!= _T('"') &&
607 (inQuotes
|| ((ch
!= _T(' ')) && (ch
!= 9) &&
608 (ch
!= _T(';')) && (ch
!= _T('%')) && (ch
!= _T('#')))))
613 ch
= (char)istr
.peek();
620 // Read }-terminated value, taking nested braces into account.
621 wxString
BibReadValue(wxString
& line
,
622 bool ignoreBraces
= true,
623 bool quotesMayTerminate
= true)
627 bool stopping
= false;
629 if (line
.length() >= 4000)
632 wxSnprintf(buf
, sizeof(buf
), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine
);
633 wxLogError(buf
, "Tex2RTF Fatal Error");
634 return wxEmptyString
;
637 while (!line
.empty() && !stopping
)
640 line
= line
.substr(1);
654 else if (quotesMayTerminate
&& ch
== _T('"'))
662 if (!ignoreBraces
|| (ch
!= _T('{') && ch
!= _T('}')))
672 void BibReadValue(wxSTD istream
& istr
, wxChar
*buffer
, bool ignoreBraces
= true,
673 bool quotesMayTerminate
= true)
678 char ch
= (char)istr
.peek();
679 bool stopping
= false;
680 while (!istr
.eof() && !stopping
)
686 wxSnprintf(buf
, sizeof(buf
), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine
);
687 wxLogError(buf
, "Tex2RTF Fatal Error");
704 else if (quotesMayTerminate
&& ch
== '"')
711 if (!ignoreBraces
|| (ch
!= '{' && ch
!= '}'))
721 wxUnusedVar(stopping
);
724 bool ReadBib(wxChar
*filename
)
726 if (!wxFileExists(filename
))
729 wxString name
= filename
;
731 wxSTD ifstream
istr((char const *)name
.fn_str(), wxSTD
ios::in
);
732 if (istr
.bad()) return false;
736 OnInform(_T("Reading .bib file..."));
739 wxChar fieldValue
[4000];
740 wxChar recordType
[100];
741 wxChar recordKey
[100];
742 wxChar recordField
[100];
747 BibEatWhiteSpace(istr
);
751 wxSnprintf(buf
, sizeof(buf
), _T("Expected @: malformed bib file at line %ld (%s)"), BibLine
, filename
);
755 BibReadWord(istr
, recordType
);
756 BibEatWhiteSpace(istr
);
758 if (ch
!= '{' && ch
!= '(')
760 wxSnprintf(buf
, sizeof(buf
), _T("Expected { or ( after record type: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
764 BibEatWhiteSpace(istr
);
765 if (StringMatch(recordType
, _T("string"), false, true))
767 BibReadWord(istr
, recordType
);
768 BibEatWhiteSpace(istr
);
772 wxSnprintf(buf
, sizeof(buf
), _T("Expected = after string key: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
776 BibEatWhiteSpace(istr
);
778 if (ch
!= '"' && ch
!= '{')
780 wxSnprintf(buf
, sizeof(buf
), _T("Expected = after string key: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
784 BibReadValue(istr
, fieldValue
);
786 // Now put in hash table if necesary
787 if (!BibStringTable
.Get(recordType
))
788 BibStringTable
.Put(recordType
, (wxObject
*)copystring(fieldValue
));
790 // Read closing ) or }
791 BibEatWhiteSpace(istr
);
793 BibEatWhiteSpace(istr
);
797 BibReadWord(istr
, recordKey
);
799 BibEntry
*bibEntry
= new BibEntry
;
800 bibEntry
->key
= copystring(recordKey
);
801 bibEntry
->type
= copystring(recordType
);
803 bool moreRecords
= true;
804 while (moreRecords
&& !istr
.eof())
806 BibEatWhiteSpace(istr
);
808 if (ch
== '}' || ch
== ')')
814 BibEatWhiteSpace(istr
);
815 BibReadWord(istr
, recordField
);
816 BibEatWhiteSpace(istr
);
820 wxSnprintf(buf
, sizeof(buf
), _T("Expected = after field type: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
824 BibEatWhiteSpace(istr
);
826 if (ch
!= '{' && ch
!= '"')
829 BibReadWord(istr
, fieldValue
+1);
831 // If in the table of strings, replace with string from table.
832 wxChar
*s
= (wxChar
*)BibStringTable
.Get(fieldValue
);
835 wxStrcpy(fieldValue
, s
);
839 BibReadValue(istr
, fieldValue
, true, (ch
== _T('"') ? true : false));
841 // Now we can add a field
842 if (StringMatch(recordField
, _T("author"), false, true))
843 bibEntry
->author
= copystring(fieldValue
);
844 else if (StringMatch(recordField
, _T("key"), false, true))
846 else if (StringMatch(recordField
, _T("annotate"), false, true))
848 else if (StringMatch(recordField
, _T("abstract"), false, true))
850 else if (StringMatch(recordField
, _T("edition"), false, true))
852 else if (StringMatch(recordField
, _T("howpublished"), false, true))
854 else if (StringMatch(recordField
, _T("note"), false, true) || StringMatch(recordField
, _T("notes"), false, true))
856 else if (StringMatch(recordField
, _T("series"), false, true))
858 else if (StringMatch(recordField
, _T("type"), false, true))
860 else if (StringMatch(recordField
, _T("keywords"), false, true))
862 else if (StringMatch(recordField
, _T("editor"), false, true) || StringMatch(recordField
, _T("editors"), false, true))
863 bibEntry
->editor
= copystring(fieldValue
);
864 else if (StringMatch(recordField
, _T("title"), false, true))
865 bibEntry
->title
= copystring(fieldValue
);
866 else if (StringMatch(recordField
, _T("booktitle"), false, true))
867 bibEntry
->booktitle
= copystring(fieldValue
);
868 else if (StringMatch(recordField
, _T("journal"), false, true))
869 bibEntry
->journal
= copystring(fieldValue
);
870 else if (StringMatch(recordField
, _T("volume"), false, true))
871 bibEntry
->volume
= copystring(fieldValue
);
872 else if (StringMatch(recordField
, _T("number"), false, true))
873 bibEntry
->number
= copystring(fieldValue
);
874 else if (StringMatch(recordField
, _T("year"), false, true))
875 bibEntry
->year
= copystring(fieldValue
);
876 else if (StringMatch(recordField
, _T("month"), false, true))
877 bibEntry
->month
= copystring(fieldValue
);
878 else if (StringMatch(recordField
, _T("pages"), false, true))
879 bibEntry
->pages
= copystring(fieldValue
);
880 else if (StringMatch(recordField
, _T("publisher"), false, true))
881 bibEntry
->publisher
= copystring(fieldValue
);
882 else if (StringMatch(recordField
, _T("address"), false, true))
883 bibEntry
->address
= copystring(fieldValue
);
884 else if (StringMatch(recordField
, _T("institution"), false, true) || StringMatch(recordField
, _T("school"), false, true))
885 bibEntry
->institution
= copystring(fieldValue
);
886 else if (StringMatch(recordField
, _T("organization"), false, true) || StringMatch(recordField
, _T("organisation"), false, true))
887 bibEntry
->organization
= copystring(fieldValue
);
888 else if (StringMatch(recordField
, _T("comment"), false, true) || StringMatch(recordField
, _T("comments"), false, true))
889 bibEntry
->comment
= copystring(fieldValue
);
890 else if (StringMatch(recordField
, _T("annote"), false, true))
891 bibEntry
->comment
= copystring(fieldValue
);
892 else if (StringMatch(recordField
, _T("chapter"), false, true))
893 bibEntry
->chapter
= copystring(fieldValue
);
896 wxSnprintf(buf
, sizeof(buf
), _T("Unrecognised bib field type %s at line %ld (%s)"), recordField
, BibLine
, filename
);
901 BibList
.Append(recordKey
, bibEntry
);
902 BibEatWhiteSpace(istr
);
908 void OutputBibItem(TexRef
*ref
, BibEntry
*bib
)
912 OnMacro(ltNUMBEREDBIBITEM
, 2, true);
913 OnArgument(ltNUMBEREDBIBITEM
, 1, true);
914 TexOutput(ref
->sectionNumber
);
915 OnArgument(ltNUMBEREDBIBITEM
, 1, false);
916 OnArgument(ltNUMBEREDBIBITEM
, 2, true);
919 OnMacro(ltBF
, 1, true);
920 OnArgument(ltBF
, 1, true);
922 TexOutput(bib
->author
);
923 OnArgument(ltBF
, 1, false);
924 OnMacro(ltBF
, 1, false);
925 if (bib
->author
&& (wxStrlen(bib
->author
) > 0) && (bib
->author
[wxStrlen(bib
->author
) - 1] != '.'))
932 TexOutput(bib
->year
);
937 TexOutput(bib
->month
);
940 if (bib
->year
|| bib
->month
)
943 if (StringMatch(bib
->type
, _T("article"), false, true))
947 TexOutput(bib
->title
);
952 OnMacro(ltIT
, 1, true);
953 OnArgument(ltIT
, 1, true);
954 TexOutput(bib
->journal
);
955 OnArgument(ltIT
, 1, false);
956 OnMacro(ltIT
, 1, false);
961 OnMacro(ltBF
, 1, true);
962 OnArgument(ltBF
, 1, true);
963 TexOutput(bib
->volume
);
964 OnArgument(ltBF
, 1, false);
965 OnMacro(ltBF
, 1, false);
970 TexOutput(bib
->number
);
975 TexOutput(_T(", pages "));
976 TexOutput(bib
->pages
);
980 else if (StringMatch(bib
->type
, _T("book"), false, true) ||
981 StringMatch(bib
->type
, _T("unpublished"), false, true) ||
982 StringMatch(bib
->type
, _T("manual"), false, true) ||
983 StringMatch(bib
->type
, _T("phdthesis"), false, true) ||
984 StringMatch(bib
->type
, _T("mastersthesis"), false, true) ||
985 StringMatch(bib
->type
, _T("misc"), false, true) ||
986 StringMatch(bib
->type
, _T("techreport"), false, true) ||
987 StringMatch(bib
->type
, _T("booklet"), false, true))
989 if (bib
->title
|| bib
->booktitle
)
991 OnMacro(ltIT
, 1, true);
992 OnArgument(ltIT
, 1, true);
993 TexOutput(bib
->title
? bib
->title
: bib
->booktitle
);
995 OnArgument(ltIT
, 1, false);
996 OnMacro(ltIT
, 1, false);
998 if (StringMatch(bib
->type
, _T("phdthesis"), false, true))
999 TexOutput(_T("PhD thesis. "));
1000 if (StringMatch(bib
->type
, _T("techreport"), false, true))
1001 TexOutput(_T("Technical report. "));
1004 TexOutput(_T("Ed. "));
1005 TexOutput(bib
->editor
);
1006 TexOutput(_T(". "));
1008 if (bib
->institution
)
1010 TexOutput(bib
->institution
);
1011 TexOutput(_T(". "));
1013 if (bib
->organization
)
1015 TexOutput(bib
->organization
);
1016 TexOutput(_T(". "));
1020 TexOutput(bib
->publisher
);
1021 TexOutput(_T(". "));
1025 TexOutput(bib
->address
);
1026 TexOutput(_T(". "));
1029 else if (StringMatch(bib
->type
, _T("inbook"), false, true) ||
1030 StringMatch(bib
->type
, _T("inproceedings"), false, true) ||
1031 StringMatch(bib
->type
, _T("incollection"), false, true) ||
1032 StringMatch(bib
->type
, _T("conference"), false, true))
1036 TexOutput(bib
->title
);
1040 TexOutput(_T(", from "));
1041 OnMacro(ltIT
, 1, true);
1042 OnArgument(ltIT
, 1, true);
1043 TexOutput(bib
->booktitle
);
1045 OnArgument(ltIT
, 1, false);
1046 OnMacro(ltIT
, 1, false);
1050 TexOutput(_T(", ed. "));
1051 TexOutput(bib
->editor
);
1056 TexOutput(bib
->publisher
);
1060 if (bib
->publisher
) TexOutput(_T(", "));
1061 else TexOutput(_T(" "));
1062 TexOutput(bib
->address
);
1064 if (bib
->publisher
|| bib
->address
)
1070 OnMacro(ltBF
, 1, true);
1071 OnArgument(ltBF
, 1, true);
1072 TexOutput(bib
->volume
);
1073 OnArgument(ltBF
, 1, false);
1074 OnMacro(ltBF
, 1, false);
1081 TexOutput(bib
->number
);
1082 TexOutput(_T(")."));
1086 TexOutput(_T(" Number "));
1087 TexOutput(bib
->number
);
1093 TexOutput(_T(" Chap. "));
1094 TexOutput(bib
->chapter
);
1098 if (bib
->chapter
) TexOutput(_T(", pages "));
1099 else TexOutput(_T(" Pages "));
1100 TexOutput(bib
->pages
);
1104 OnArgument(ltNUMBEREDBIBITEM
, 2, false);
1105 OnMacro(ltNUMBEREDBIBITEM
, 2, false);
1108 void OutputBib(void)
1110 // Write the heading
1111 ForceTopicName(_T("bibliography"));
1112 FakeCurrentSection(ReferencesNameString
);
1113 ForceTopicName(NULL
);
1115 OnMacro(ltPAR
, 0, true);
1116 OnMacro(ltPAR
, 0, false);
1118 if ((convertMode
== TEX_RTF
) && !winHelp
)
1120 OnMacro(ltPAR
, 0, true);
1121 OnMacro(ltPAR
, 0, false);
1124 wxStringListNode
*node
= CitationList
.GetFirst();
1127 wxChar
*citeKey
= (wxChar
*)node
->GetData();
1128 // wxNode *texNode = TexReferences.Find(citeKey);
1129 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
1130 wxNode
*bibNode
= BibList
.Find(citeKey
);
1133 BibEntry
*entry
= (BibEntry
*)bibNode
->GetData();
1134 OutputBibItem(ref
, entry
);
1136 node
= node
->GetNext();
1140 static int citeCount
= 1;
1142 void ResolveBibReferences(void)
1144 if (CitationList
.GetCount() > 0)
1145 OnInform(_T("Resolving bibliographic references..."));
1149 wxStringListNode
*node
= CitationList
.GetFirst();
1153 wxChar
*citeKey
= (wxChar
*)node
->GetData();
1154 // wxNode *texNode = TexReferences.Find(citeKey);
1155 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
1156 wxNode
*bibNode
= BibList
.Find(citeKey
);
1160 //BibEntry *entry = (BibEntry *)bibNode->GetData();
1161 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
1162 wxSnprintf(buf
, sizeof(buf
), _T("[%d]"), citeCount
);
1163 ref
->sectionNumber
= copystring(buf
);
1168 wxSnprintf(buf
, sizeof(buf
), _T("Warning: bib ref %s not resolved."), citeKey
);
1171 node
= node
->GetNext();
1175 // Remember we need to resolve this citation
1176 void AddCitation(wxChar
*citeKey
)
1178 if (!CitationList
.Member(citeKey
))
1179 CitationList
.Add(citeKey
);
1181 if (!TexReferences
.Get(citeKey
))
1183 TexReferences
.Put(citeKey
, new TexRef(citeKey
, _T("??"), NULL
));
1187 TexRef
*FindReference(wxChar
*key
)
1189 return (TexRef
*)TexReferences
.Get(key
);
1193 * Custom macro stuff
1197 bool StringTobool(const wxString
& val
)
1202 if (up
.IsSameAs(_T("YES")) ||
1203 up
.IsSameAs(_T("ON")) ||
1204 up
.IsSameAs(_T("OK")) |
1205 up
.IsSameAs(_T("1")))
1211 void RegisterIntSetting (const wxString
& s
, int *number
)
1221 // Define a variable value from the .ini file
1222 wxChar
*RegisterSetting(const wxString
& settingName
, const wxString
& settingValue
, bool interactive
)
1224 wxString
settingValueStr( settingValue
);
1226 static wxChar errorCode
[100];
1227 wxStrcpy(errorCode
, _T("OK"));
1228 if (StringMatch(settingName
, _T("chapterName"), false, true))
1230 delete[] ChapterNameString
;
1231 ChapterNameString
= copystring(settingValue
);
1233 else if (StringMatch(settingName
, _T("sectionName"), false, true))
1235 delete[] SectionNameString
;
1236 SectionNameString
= copystring(settingValue
);
1238 else if (StringMatch(settingName
, _T("subsectionName"), false, true))
1240 delete[] SubsectionNameString
;
1241 SubsectionNameString
= copystring(settingValue
);
1243 else if (StringMatch(settingName
, _T("subsubsectionName"), false, true))
1245 delete[] SubsubsectionNameString
;
1246 SubsubsectionNameString
= copystring(settingValue
);
1248 else if (StringMatch(settingName
, _T("indexName"), false, true))
1250 delete[] IndexNameString
;
1251 IndexNameString
= copystring(settingValue
);
1253 else if (StringMatch(settingName
, _T("contentsName"), false, true))
1255 delete[] ContentsNameString
;
1256 ContentsNameString
= copystring(settingValue
);
1258 else if (StringMatch(settingName
, _T("glossaryName"), false, true))
1260 delete[] GlossaryNameString
;
1261 GlossaryNameString
= copystring(settingValue
);
1263 else if (StringMatch(settingName
, _T("referencesName"), false, true))
1265 delete[] ReferencesNameString
;
1266 ReferencesNameString
= copystring(settingValue
);
1268 else if (StringMatch(settingName
, _T("tablesName"), false, true))
1270 delete[] TablesNameString
;
1271 TablesNameString
= copystring(settingValue
);
1273 else if (StringMatch(settingName
, _T("figuresName"), false, true))
1275 delete[] FiguresNameString
;
1276 FiguresNameString
= copystring(settingValue
);
1278 else if (StringMatch(settingName
, _T("tableName"), false, true))
1280 delete[] TableNameString
;
1281 TableNameString
= copystring(settingValue
);
1283 else if (StringMatch(settingName
, _T("figureName"), false, true))
1285 delete[] FigureNameString
;
1286 FigureNameString
= copystring(settingValue
);
1288 else if (StringMatch(settingName
, _T("abstractName"), false, true))
1290 delete[] AbstractNameString
;
1291 AbstractNameString
= copystring(settingValue
);
1293 else if (StringMatch(settingName
, _T("chapterFontSize"), false, true))
1294 RegisterIntSetting(settingValueStr
, &chapterFont
);
1295 else if (StringMatch(settingName
, _T("sectionFontSize"), false, true))
1296 RegisterIntSetting(settingValueStr
, §ionFont
);
1297 else if (StringMatch(settingName
, _T("subsectionFontSize"), false, true))
1298 RegisterIntSetting(settingValueStr
, &subsectionFont
);
1299 else if (StringMatch(settingName
, _T("titleFontSize"), false, true))
1300 RegisterIntSetting(settingValueStr
, &titleFont
);
1301 else if (StringMatch(settingName
, _T("authorFontSize"), false, true))
1302 RegisterIntSetting(settingValueStr
, &authorFont
);
1303 else if (StringMatch(settingName
, _T("ignoreInput"), false, true))
1304 IgnorableInputFiles
.Add(wxFileNameFromPath(settingValue
));
1305 else if (StringMatch(settingName
, _T("mirrorMargins"), false, true))
1306 mirrorMargins
= StringTobool(settingValue
);
1307 else if (StringMatch(settingName
, _T("runTwice"), false, true))
1308 runTwice
= StringTobool(settingValue
);
1309 else if (StringMatch(settingName
, _T("isInteractive"), false, true))
1310 isInteractive
= StringTobool(settingValue
);
1311 else if (StringMatch(settingName
, _T("headerRule"), false, true))
1312 headerRule
= StringTobool(settingValue
);
1313 else if (StringMatch(settingName
, _T("footerRule"), false, true))
1314 footerRule
= StringTobool(settingValue
);
1315 else if (StringMatch(settingName
, _T("combineSubSections"), false, true))
1316 combineSubSections
= StringTobool(settingValue
);
1317 else if (StringMatch(settingName
, _T("listLabelIndent"), false, true))
1318 RegisterIntSetting(settingValueStr
, &labelIndentTab
);
1319 else if (StringMatch(settingName
, _T("listItemIndent"), false, true))
1320 RegisterIntSetting(settingValueStr
, &itemIndentTab
);
1321 else if (StringMatch(settingName
, _T("useUpButton"), false, true))
1322 useUpButton
= StringTobool(settingValue
);
1323 else if (StringMatch(settingName
, _T("useHeadingStyles"), false, true))
1324 useHeadingStyles
= StringTobool(settingValue
);
1325 else if (StringMatch(settingName
, _T("useWord"), false, true))
1326 useWord
= StringTobool(settingValue
);
1327 else if (StringMatch(settingName
, _T("contentsDepth"), false, true))
1328 RegisterIntSetting(settingValueStr
, &contentsDepth
);
1329 else if (StringMatch(settingName
, _T("generateHPJ"), false, true))
1330 generateHPJ
= StringTobool(settingValue
);
1331 else if (StringMatch(settingName
, _T("truncateFilenames"), false, true))
1332 truncateFilenames
= StringTobool(settingValue
);
1333 else if (StringMatch(settingName
, _T("winHelpVersion"), false, true))
1334 RegisterIntSetting(settingValueStr
, &winHelpVersion
);
1335 else if (StringMatch(settingName
, _T("winHelpContents"), false, true))
1336 winHelpContents
= StringTobool(settingValue
);
1337 else if (StringMatch(settingName
, _T("htmlIndex"), false, true))
1338 htmlIndex
= StringTobool(settingValue
);
1339 else if (StringMatch(settingName
, _T("htmlWorkshopFiles"), false, true))
1340 htmlWorkshopFiles
= StringTobool(settingValue
);
1341 else if (StringMatch(settingName
, _T("htmlFrameContents"), false, true))
1342 htmlFrameContents
= StringTobool(settingValue
);
1343 else if (StringMatch(settingName
, _T("htmlStylesheet"), false, true))
1346 delete[] htmlStylesheet
;
1347 htmlStylesheet
= copystring(settingValue
);
1349 else if (StringMatch(settingName
, _T("upperCaseNames"), false, true))
1350 upperCaseNames
= StringTobool(settingValue
);
1351 else if (StringMatch(settingName
, _T("ignoreBadRefs"), false, true))
1352 ignoreBadRefs
= StringTobool(settingValue
);
1353 else if (StringMatch(settingName
, _T("htmlFaceName"), false, true))
1355 delete[] htmlFaceName
;
1356 htmlFaceName
= copystring(settingValue
);
1358 else if (StringMatch(settingName
, _T("winHelpTitle"), false, true))
1361 delete[] winHelpTitle
;
1362 winHelpTitle
= copystring(settingValue
);
1364 else if (StringMatch(settingName
, _T("indexSubsections"), false, true))
1365 indexSubsections
= StringTobool(settingValue
);
1366 else if (StringMatch(settingName
, _T("compatibility"), false, true))
1367 compatibilityMode
= StringTobool(settingValue
);
1368 else if (StringMatch(settingName
, _T("defaultColumnWidth"), false, true))
1370 RegisterIntSetting(settingValueStr
, &defaultTableColumnWidth
);
1371 defaultTableColumnWidth
= 20*defaultTableColumnWidth
;
1373 else if (StringMatch(settingName
, _T("bitmapMethod"), false, true))
1375 if ((wxStrcmp(settingValue
, _T("includepicture")) != 0) && (wxStrcmp(settingValue
, _T("hex")) != 0) &&
1376 (wxStrcmp(settingValue
, _T("import")) != 0))
1379 OnError(_T("Unknown bitmapMethod"));
1380 wxStrcpy(errorCode
, _T("Unknown bitmapMethod"));
1384 delete[] bitmapMethod
;
1385 bitmapMethod
= copystring(settingValue
);
1388 else if (StringMatch(settingName
, _T("htmlBrowseButtons"), false, true))
1390 if (wxStrcmp(settingValue
, _T("none")) == 0)
1391 htmlBrowseButtons
= HTML_BUTTONS_NONE
;
1392 else if (wxStrcmp(settingValue
, _T("bitmap")) == 0)
1393 htmlBrowseButtons
= HTML_BUTTONS_BITMAP
;
1394 else if (wxStrcmp(settingValue
, _T("text")) == 0)
1395 htmlBrowseButtons
= HTML_BUTTONS_TEXT
;
1399 OnInform(_T("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text."));
1400 wxStrcpy(errorCode
, _T("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text."));
1403 else if (StringMatch(settingName
, _T("backgroundImage"), false, true))
1405 backgroundImageString
= copystring(settingValue
);
1407 else if (StringMatch(settingName
, _T("backgroundColour"), false, true))
1409 delete[] backgroundColourString
;
1410 backgroundColourString
= copystring(settingValue
);
1412 else if (StringMatch(settingName
, _T("textColour"), false, true))
1414 textColourString
= copystring(settingValue
);
1416 else if (StringMatch(settingName
, _T("linkColour"), false, true))
1418 linkColourString
= copystring(settingValue
);
1420 else if (StringMatch(settingName
, _T("followedLinkColour"), false, true))
1422 followedLinkColourString
= copystring(settingValue
);
1424 else if (StringMatch(settingName
, _T("conversionMode"), false, true))
1426 if (StringMatch(settingValue
, _T("RTF"), false, true))
1428 winHelp
= false; convertMode
= TEX_RTF
;
1430 else if (StringMatch(settingValue
, _T("WinHelp"), false, true))
1432 winHelp
= true; convertMode
= TEX_RTF
;
1434 else if (StringMatch(settingValue
, _T("XLP"), false, true) ||
1435 StringMatch(settingValue
, _T("wxHelp"), false, true))
1437 convertMode
= TEX_XLP
;
1439 else if (StringMatch(settingValue
, _T("HTML"), false, true))
1441 convertMode
= TEX_HTML
;
1446 OnInform(_T("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML."));
1447 wxStrcpy(errorCode
, _T("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML."));
1450 else if (StringMatch(settingName
, _T("documentFontSize"), false, true))
1453 RegisterIntSetting(settingValueStr
, &n
);
1454 if (n
== 10 || n
== 11 || n
== 12)
1459 wxSnprintf(buf
, sizeof(buf
), _T("Initialisation file error: nonstandard document font size %d."), n
);
1462 wxStrcpy(errorCode
, buf
);
1468 wxSnprintf(buf
, sizeof(buf
), _T("Initialisation file error: unrecognised setting %s."), settingName
.c_str());
1471 wxStrcpy(errorCode
, buf
);
1476 bool ReadCustomMacros(const wxString
& filename
)
1478 if (!wxFileExists(filename
))
1481 wxFileInputStream
input( filename
);
1482 if(!input
.Ok()) return false;
1483 wxTextInputStream
ini( input
);
1485 CustomMacroList
.Clear();
1487 while (!input
.Eof())
1489 wxString line
= ini
.ReadLine();
1490 BibEatWhiteSpace(line
);
1491 if (line
.empty()) continue;
1493 if (line
[0] != _T('\\')) // Not a macro definition, so must be NAME=VALUE
1495 wxString settingName
= BibReadWord(line
);
1496 BibEatWhiteSpace(line
);
1497 if (line
.empty() || line
[0] != _T('='))
1499 OnError(_T("Expected = following name: malformed tex2rtf.ini file."));
1504 line
= line
.substr(1);
1505 BibEatWhiteSpace(line
);
1506 wxString settingValue
= BibReadToEOL(line
);
1507 RegisterSetting(settingName
, settingValue
);
1512 line
= line
.substr(1);
1513 wxString macroName
= BibReadWord(line
);
1514 BibEatWhiteSpace(line
);
1515 if (line
[0] != _T('['))
1517 OnError(_T("Expected [ followed by number of arguments: malformed tex2rtf.ini file."));
1520 line
= line
.substr(1);
1521 wxString noAargStr
= line
.BeforeFirst(_T(']'));
1522 line
= line
.AfterFirst(_T(']'));
1524 if (!noAargStr
.ToLong(&noArgs
) || line
.empty())
1526 OnError(_T("Expected ] following number of arguments: malformed tex2rtf.ini file."));
1529 BibEatWhiteSpace(line
);
1530 if (line
[0] != _T('{'))
1532 OnError(_T("Expected { followed by macro body: malformed tex2rtf.ini file."));
1536 CustomMacro
*macro
= new CustomMacro(macroName
.c_str(), noArgs
, NULL
);
1537 wxString macroBody
= BibReadValue(line
, false, false); // Don't ignore extra braces
1538 if (!macroBody
.empty())
1539 macro
->macroBody
= copystring(macroBody
.c_str());
1541 BibEatWhiteSpace(line
);
1542 CustomMacroList
.Append(macroName
.c_str(), macro
);
1543 AddMacroDef(ltCUSTOM_MACRO
, macroName
.c_str(), noArgs
);
1548 wxSnprintf(mbuf
, sizeof(mbuf
), _T("Read initialization file %s."), filename
.c_str());
1553 CustomMacro
*FindCustomMacro(wxChar
*name
)
1555 wxNode
*node
= CustomMacroList
.Find(name
);
1558 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
1564 // Display custom macros
1565 void ShowCustomMacros(void)
1567 wxNode
*node
= CustomMacroList
.GetFirst();
1570 OnInform(_T("No custom macros loaded.\n"));
1577 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
1578 wxSnprintf(buf
, sizeof(buf
), _T("\\%s[%d]\n {%s}"), macro
->macroName
, macro
->noArgs
,
1579 macro
->macroBody
? macro
->macroBody
: _T(""));
1581 node
= node
->GetNext();
1585 // Parse a string into several comma-separated fields
1586 wxChar
*ParseMultifieldString(wxChar
*allFields
, int *pos
)
1588 static wxChar buffer
[300];
1590 int fieldIndex
= *pos
;
1591 int len
= wxStrlen(allFields
);
1593 bool keepGoing
= true;
1594 while ((fieldIndex
<= len
) && keepGoing
)
1596 if (allFields
[fieldIndex
] == _T(' '))
1601 else if (allFields
[fieldIndex
] == _T(','))
1603 *pos
= fieldIndex
+ 1;
1606 else if (allFields
[fieldIndex
] == 0)
1608 *pos
= fieldIndex
+ 1;
1613 buffer
[i
] = allFields
[fieldIndex
];
1619 if (oldPos
== (*pos
))
1633 ColourTableEntry::ColourTableEntry(const wxChar
*theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1635 name
= copystring(theName
);
1641 ColourTableEntry::~ColourTableEntry(void)
1646 void AddColour(const wxChar
*theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1648 wxNode
*node
= ColourTable
.Find(theName
);
1651 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->GetData();
1652 if (entry
->red
== r
|| entry
->green
== g
|| entry
->blue
== b
)
1660 ColourTableEntry
*entry
= new ColourTableEntry(theName
, r
, g
, b
);
1661 ColourTable
.Append(theName
, entry
);
1664 int FindColourPosition(wxChar
*theName
)
1667 wxNode
*node
= ColourTable
.GetFirst();
1670 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->GetData();
1671 if (wxStrcmp(theName
, entry
->name
) == 0)
1674 node
= node
->GetNext();
1679 // Converts e.g. "red" -> "#FF0000"
1680 extern void DecToHex(int, wxChar
*);
1681 bool FindColourHTMLString(wxChar
*theName
, wxChar
*buf
)
1683 wxNode
*node
= ColourTable
.GetFirst();
1686 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->GetData();
1687 if (wxStrcmp(theName
, entry
->name
) == 0)
1689 wxStrcpy(buf
, _T("#"));
1692 DecToHex(entry
->red
, buf2
);
1693 wxStrcat(buf
, buf2
);
1694 DecToHex(entry
->green
, buf2
);
1695 wxStrcat(buf
, buf2
);
1696 DecToHex(entry
->blue
, buf2
);
1697 wxStrcat(buf
, buf2
);
1701 node
= node
->GetNext();
1707 void InitialiseColourTable(void)
1709 // \\red0\\green0\\blue0;
1710 AddColour(_T("black"), 0,0,0);
1712 // \\red0\\green0\\blue255;\\red0\\green255\\blue255;\n");
1713 AddColour(_T("cyan"), 0,255,255);
1715 // \\red0\\green255\\blue0;
1716 AddColour(_T("green"), 0,255,0);
1718 // \\red255\\green0\\blue255;
1719 AddColour(_T("magenta"), 255,0,255);
1721 // \\red255\\green0\\blue0;
1722 AddColour(_T("red"), 255,0,0);
1724 // \\red255\\green255\\blue0;
1725 AddColour(_T("yellow"), 255,255,0);
1727 // \\red255\\green255\\blue255;}");
1728 AddColour(_T("white"), 255,255,255);
1732 * The purpose of this is to reduce the number of times wxYield is
1733 * called, since under Windows this can slow things down.
1736 void Tex2RTFYield(bool force
)
1739 static int yieldCount
= 0;
1746 if (yieldCount
== 0)
1758 // In both RTF generation and HTML generation for wxHelp version 2,
1759 // we need to associate \indexed keywords with the current filename/topics.
1761 // Hash table for lists of keywords for topics (WinHelp).
1762 wxHashTable
TopicTable(wxKEY_STRING
);
1763 void AddKeyWordForTopic(wxChar
*topic
, wxChar
*entry
, wxChar
*filename
)
1765 TexTopic
*texTopic
= (TexTopic
*)TopicTable
.Get(topic
);
1768 texTopic
= new TexTopic(filename
);
1769 texTopic
->keywords
= new wxStringList
;
1770 TopicTable
.Put(topic
, texTopic
);
1773 if (!texTopic
->keywords
->Member(entry
))
1774 texTopic
->keywords
->Add(entry
);
1777 void ClearKeyWordTable(void)
1779 TopicTable
.BeginFind();
1780 wxHashTable::Node
*node
= TopicTable
.Next();
1783 TexTopic
*texTopic
= (TexTopic
*)node
->GetData();
1785 node
= TopicTable
.Next();
1792 * TexTopic structure
1795 TexTopic::TexTopic(wxChar
*f
)
1798 filename
= copystring(f
);
1801 hasChildren
= false;
1805 TexTopic::~TexTopic(void)
1813 // Convert case, according to upperCaseNames setting.
1814 wxChar
*ConvertCase(wxChar
*s
)
1816 static wxChar buf
[256];
1817 int len
= wxStrlen(s
);
1820 for (i
= 0; i
< len
; i
++)
1821 buf
[i
] = (wxChar
)wxToupper(s
[i
]);
1823 for (i
= 0; i
< len
; i
++)
1824 buf
[i
] = (wxChar
)wxTolower(s
[i
]);
1829 // if substring is true, search for str1 in str2
1830 bool StringMatch(const wxChar
*str1
, const wxChar
*str2
, bool subString
,
1835 wxString
Sstr1(str1
);
1836 wxString
Sstr2(str2
);
1842 return Sstr2
.Index(Sstr1
) != (size_t)wxNOT_FOUND
;
1845 return exact
? wxString(str2
).Cmp(str1
) == 0 :
1846 wxString(str2
).CmpNoCase(str1
) == 0;