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"
32 #include "wx/beforestd.h"
41 #include "wx/afterstd.h"
46 static inline wxChar
* copystring(const wxChar
* s
)
47 { return wxStrcpy(new wxChar
[wxStrlen(s
) + 1], s
); }
49 wxHashTable
TexReferences(wxKEY_STRING
);
50 wxList
BibList(wxKEY_STRING
);
51 wxStringList CitationList
;
52 wxList
ColourTable(wxKEY_STRING
);
53 wxHashTable
BibStringTable(wxKEY_STRING
);
54 wxList
CustomMacroList(wxKEY_STRING
);
55 TexChunk
*currentSection
= NULL
;
56 wxChar
*fakeCurrentSection
= NULL
;
58 static long BibLine
= 1;
60 void OutputCurrentSection(void)
62 if (fakeCurrentSection
)
63 TexOutput(fakeCurrentSection
);
64 else if (currentSection
)
65 TraverseChildrenFromChunk(currentSection
);
68 // Nasty but the way things are done now, necessary,
69 // in order to output a chunk properly to a string (macros and all).
70 void OutputCurrentSectionToString(wxChar
*buf
)
72 if (fakeCurrentSection
)
73 wxStrcpy(buf
, fakeCurrentSection
);
75 OutputChunkToString(currentSection
, buf
);
78 void OutputChunkToString(TexChunk
*chunk
, wxChar
*buf
)
80 FILE *tempfd
= wxFopen(_T("tmp.tmp"), _T("w"));
84 FILE *old1
= CurrentOutput1
;
85 FILE *old2
= CurrentOutput2
;
87 CurrentOutput1
= tempfd
;
88 CurrentOutput2
= NULL
;
90 TraverseChildrenFromChunk(chunk
);
92 CurrentOutput1
= old1
;
93 CurrentOutput2
= old2
;
97 // Read from file into string
98 tempfd
= wxFopen(_T("tmp.tmp"), _T("r"));
117 wxRemoveFile(_T("tmp.tmp"));
120 // Called by Tex2Any to simulate a section
121 void FakeCurrentSection(wxChar
*fakeSection
, bool addToContents
)
123 currentSection
= NULL
;
124 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
125 fakeCurrentSection
= copystring(fakeSection
);
127 if (DocumentStyle
== LATEX_ARTICLE
)
129 int mac
= ltSECTIONHEADING
;
131 mac
= ltSECTIONHEADINGSTAR
;
132 OnMacro(mac
, 0, true);
133 OnMacro(mac
, 0, false);
137 int mac
= ltCHAPTERHEADING
;
139 mac
= ltCHAPTERHEADINGSTAR
;
140 OnMacro(mac
, 0, true);
141 OnMacro(mac
, 0, false);
143 if (fakeCurrentSection
) delete[] fakeCurrentSection
;
144 fakeCurrentSection
= NULL
;
147 // Look for \label macro, use this ref name if found or
148 // make up a topic name otherwise.
149 static long topicCounter
= 0;
151 void ResetTopicCounter(void)
156 static wxChar
*forceTopicName
= NULL
;
158 void ForceTopicName(const wxChar
*name
)
161 delete[] forceTopicName
;
163 forceTopicName
= copystring(name
);
165 forceTopicName
= NULL
;
168 wxChar
*FindTopicName(TexChunk
*chunk
)
171 return forceTopicName
;
173 wxChar
*topicName
= NULL
;
174 static wxChar topicBuf
[100];
176 if (chunk
&& (chunk
->type
== CHUNK_TYPE_MACRO
) &&
177 (chunk
->macroId
== ltLABEL
))
179 wxNode
*node
= chunk
->children
.GetFirst();
182 TexChunk
*child
= (TexChunk
*)node
->GetData();
183 if (child
->type
== CHUNK_TYPE_ARG
)
185 wxNode
*snode
= child
->children
.GetFirst();
188 TexChunk
*schunk
= (TexChunk
*)snode
->GetData();
189 if (schunk
->type
== CHUNK_TYPE_STRING
)
190 topicName
= schunk
->value
;
199 wxSnprintf(topicBuf
, sizeof(topicBuf
), _T("topic%ld"), topicCounter
);
206 * Simulate argument data, so we can 'drive' clients which implement
207 * certain basic formatting behaviour.
208 * Snag is that some save a TexChunk, so don't use yet...
212 void StartSimulateArgument(const wxChar
*data
)
214 wxStrcpy(currentArgData
, data
);
218 void EndSimulateArgument(void)
224 * Parse and convert unit arguments to points
228 int ParseUnitArgument(const wxChar
*unitArg_
)
230 wxWxCharBuffer
unitBuf(unitArg_
);
231 wxChar
*unitArg
= unitBuf
.data();
233 float conversionFactor
= 1.0;
234 float unitValue
= 0.0;
235 int len
= wxStrlen(unitArg
);
236 // Get rid of any accidentally embedded commands
237 for (int i
= 0; i
< len
; i
++)
238 if (unitArg
[i
] == '\\')
240 len
= wxStrlen(unitArg
);
242 if (unitArg
&& (len
> 0) && (isdigit(unitArg
[0]) || unitArg
[0] == '-'))
244 wxSscanf(unitArg
, _T("%f"), &unitValue
);
248 units
[0] = unitArg
[len
-2];
249 units
[1] = unitArg
[len
-1];
251 if (wxStrcmp(units
, _T("in")) == 0)
252 conversionFactor
= 72.0;
253 else if (wxStrcmp(units
, _T("cm")) == 0)
254 conversionFactor
= (float)72.0/(float)2.51;
255 else if (wxStrcmp(units
, _T("mm")) == 0)
256 conversionFactor
= (float)72.0/(float)25.1;
257 else if (wxStrcmp(units
, _T("pt")) == 0)
258 conversionFactor
= 1;
260 return (int)(unitValue
*conversionFactor
);
266 * Strip off any extension (dot something) from end of file,
267 * IF one exists. Inserts zero into buffer.
271 void StripExtension(wxChar
*buffer
)
273 int len
= wxStrlen(buffer
);
277 if (buffer
[i
] == '.')
291 void SetFontSizes(int pointSize
)
343 void AddTexRef(wxChar
*name
, wxChar
*file
, wxChar
*sectionName
,
344 int chapter
, int section
, int subsection
, int subsubsection
)
346 TexRef
*texRef
= (TexRef
*)TexReferences
.Get(name
);
347 if (texRef
) TexReferences
.Delete(name
);
354 wxStrcat(buf, sectionName);
361 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), chapter
);
368 wxStrcat(buf
, _T("."));
370 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), section
);
376 wxStrcat(buf
, _T("."));
377 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), subsection
);
383 wxStrcat(buf
, _T("."));
384 wxSnprintf(buf2
, sizeof(buf2
), _T("%d"), subsubsection
);
387 wxChar
*tmp
= ((wxStrlen(buf
) > 0) ? buf
: (wxChar
*)NULL
);
388 TexReferences
.Put(name
, new TexRef(name
, file
, tmp
, sectionName
));
391 void WriteTexReferences(wxChar
*filename
)
393 wxString name
= filename
;
396 if (!(wxFileExists(name
)?file
.Open(name
):file
.Create(name
)))
401 TexReferences
.BeginFind();
402 wxHashTable::Node
*node
= TexReferences
.Next();
406 TexRef
*ref
= (TexRef
*)node
->GetData();
407 wxString converter
= ref
->refLabel
;
408 converter
<< wxT(" ");
409 converter
<< (ref
->refFile
? ref
->refFile
: _T("??"));
410 converter
<< wxT(" ");
411 converter
<< (ref
->sectionName
? ref
->sectionName
: _T("??")) ;
412 converter
<< wxT(" ");
413 converter
<< (ref
->sectionNumber
? ref
->sectionNumber
: _T("??")) ;
414 file
.AddLine(converter
);
416 if (!ref
->sectionNumber
|| (wxStrcmp(ref
->sectionNumber
, _T("??")) == 0 && wxStrcmp(ref
->sectionName
, _T("??")) == 0))
419 wxSnprintf(buf
, sizeof(buf
), _T("Warning: reference %s not resolved."), ref
->refLabel
);
422 node
= TexReferences
.Next();
429 void ReadTexReferences(wxChar
*filename
)
431 wxString name
= filename
;
433 if (!wxFileExists(name
))
437 if (!file
.Open(name
))
441 for ( line
= file
.GetFirstLine(); !file
.Eof(); line
= file
.GetNextLine() )
443 wxString labelStr
= line
.BeforeFirst(wxT(' '));
444 line
= line
.AfterFirst(wxT(' '));
445 wxString fileStr
= line
.BeforeFirst(wxT(' '));
446 line
= line
.AfterFirst(wxT(' '));
447 wxString sectionNameStr
= line
.BeforeFirst(wxT(' '));
448 wxString sectionStr
= line
.AfterFirst(wxT(' '));
450 // gt - needed to trick the hash table "TexReferences" into deleting the key
451 // strings it creates in the Put() function, but not the item that is
452 // created here, as that is destroyed elsewhere. Without doing this, there
453 // were massive memory leaks
454 TexReferences
.DeleteContents(true);
461 sectionNameStr
.c_str()
464 TexReferences
.DeleteContents(false);
470 * Bibliography-handling code
474 void BibEatWhiteSpace(wxString
& line
)
476 while(!line
.empty() && (line
[0] == _T(' ') || line
[0] == _T('\t') || line
[0] == (wxChar
)EOF
))
480 line
= line
.substr(1);
483 // Ignore end-of-line comments
484 if ( !line
.empty() && (line
[0] == _T('%') || line
[0] == _T(';') || line
[0] == _T('#')))
490 void BibEatWhiteSpace(wxSTD istream
& str
)
492 char ch
= (char)str
.peek();
494 while (!str
.eof() && (ch
== ' ' || ch
== '\t' || ch
== 13 || ch
== 10 || ch
== (char)EOF
))
499 if ((ch
== (char)EOF
) || str
.eof()) return;
500 ch
= (char)str
.peek();
503 // Ignore end-of-line comments
504 if (ch
== '%' || ch
== ';' || ch
== '#')
507 ch
= (char)str
.peek();
508 while (ch
!= 10 && ch
!= 13 && !str
.eof())
511 ch
= (char)str
.peek();
513 BibEatWhiteSpace(str
);
517 // Read word up to { or , or space
518 wxString
BibReadWord(wxString
& line
)
522 while (!line
.empty() &&
523 line
[0] != _T('\t') &&
524 line
[0] != _T(' ') &&
525 line
[0] != _T('{') &&
526 line
[0] != _T('(') &&
527 line
[0] != _T(',') &&
531 line
= line
.substr(1);
536 void BibReadWord(wxSTD istream
& istr
, wxChar
*buffer
)
540 char ch
= (char)istr
.peek();
541 while (!istr
.eof() && ch
!= ' ' && ch
!= '{' && ch
!= '(' && ch
!= 13 && ch
!= 10 && ch
!= '\t' &&
542 ch
!= ',' && ch
!= '=')
547 ch
= (char)istr
.peek();
552 // Read string (double-quoted or not) to end quote or EOL
553 wxString
BibReadToEOL(wxString
& line
)
556 return wxEmptyString
;
559 bool inQuotes
= false;
560 if (line
[0] == _T('"'))
562 line
= line
.substr(1);
565 // If in quotes, read white space too. If not,
566 // stop at white space or comment.
567 while (!line
.empty() && line
[0] != _T('"') &&
568 (inQuotes
|| ((line
[0] != _T(' ')) && (line
[0] != '\t') &&
569 (line
[0] != _T(';')) && (line
[0] != _T('%')) && (line
[0] != _T('#')))))
572 line
= line
.substr(1);
574 if (!line
.empty() && line
[0] == '"')
575 line
= line
.substr(1);
580 void BibReadToEOL(wxSTD istream
& istr
, wxChar
*buffer
)
584 char ch
= (char)istr
.peek();
585 bool inQuotes
= false;
589 ch
= (char)istr
.peek();
592 // If in quotes, read white space too. If not,
593 // stop at white space or comment.
594 while (!istr
.eof() && ch
!= 13 && ch
!= 10 && ch
!= _T('"') &&
595 (inQuotes
|| ((ch
!= _T(' ')) && (ch
!= 9) &&
596 (ch
!= _T(';')) && (ch
!= _T('%')) && (ch
!= _T('#')))))
601 ch
= (char)istr
.peek();
608 // Read }-terminated value, taking nested braces into account.
609 wxString
BibReadValue(wxString
& line
,
610 bool ignoreBraces
= true,
611 bool quotesMayTerminate
= true)
615 bool stopping
= false;
617 if (line
.length() >= 4000)
620 wxSnprintf(buf
, sizeof(buf
), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine
);
621 wxLogError(buf
, "Tex2RTF Fatal Error");
622 return wxEmptyString
;
625 while (!line
.empty() && !stopping
)
628 line
= line
.substr(1);
642 else if (quotesMayTerminate
&& ch
== _T('"'))
650 if (!ignoreBraces
|| (ch
!= _T('{') && ch
!= _T('}')))
660 void BibReadValue(wxSTD istream
& istr
, wxChar
*buffer
, bool ignoreBraces
= true,
661 bool quotesMayTerminate
= true)
666 char ch
= (char)istr
.peek();
667 bool stopping
= false;
668 while (!istr
.eof() && !stopping
)
674 wxSnprintf(buf
, sizeof(buf
), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine
);
675 wxLogError(buf
, "Tex2RTF Fatal Error");
692 else if (quotesMayTerminate
&& ch
== '"')
699 if (!ignoreBraces
|| (ch
!= '{' && ch
!= '}'))
709 wxUnusedVar(stopping
);
712 bool ReadBib(const wxChar
*filename
)
714 if (!wxFileExists(filename
))
717 wxString name
= filename
;
719 wxSTD ifstream
istr((char const *)name
.fn_str(), wxSTD
ios::in
);
720 if (istr
.bad()) return false;
724 OnInform(_T("Reading .bib file..."));
727 wxChar fieldValue
[4000];
728 wxChar recordType
[100];
729 wxChar recordKey
[100];
730 wxChar recordField
[100];
735 BibEatWhiteSpace(istr
);
739 wxSnprintf(buf
, sizeof(buf
), _T("Expected @: malformed bib file at line %ld (%s)"), BibLine
, filename
);
743 BibReadWord(istr
, recordType
);
744 BibEatWhiteSpace(istr
);
746 if (ch
!= '{' && ch
!= '(')
748 wxSnprintf(buf
, sizeof(buf
), _T("Expected { or ( after record type: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
752 BibEatWhiteSpace(istr
);
753 if (StringMatch(recordType
, _T("string"), false, true))
755 BibReadWord(istr
, recordType
);
756 BibEatWhiteSpace(istr
);
760 wxSnprintf(buf
, sizeof(buf
), _T("Expected = after string key: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
764 BibEatWhiteSpace(istr
);
766 if (ch
!= '"' && ch
!= '{')
768 wxSnprintf(buf
, sizeof(buf
), _T("Expected = after string key: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
772 BibReadValue(istr
, fieldValue
);
774 // Now put in hash table if necesary
775 if (!BibStringTable
.Get(recordType
))
776 BibStringTable
.Put(recordType
, (wxObject
*)copystring(fieldValue
));
778 // Read closing ) or }
779 BibEatWhiteSpace(istr
);
781 BibEatWhiteSpace(istr
);
785 BibReadWord(istr
, recordKey
);
787 BibEntry
*bibEntry
= new BibEntry
;
788 bibEntry
->key
= copystring(recordKey
);
789 bibEntry
->type
= copystring(recordType
);
791 bool moreRecords
= true;
792 while (moreRecords
&& !istr
.eof())
794 BibEatWhiteSpace(istr
);
796 if (ch
== '}' || ch
== ')')
802 BibEatWhiteSpace(istr
);
803 BibReadWord(istr
, recordField
);
804 BibEatWhiteSpace(istr
);
808 wxSnprintf(buf
, sizeof(buf
), _T("Expected = after field type: malformed .bib file at line %ld (%s)"), BibLine
, filename
);
812 BibEatWhiteSpace(istr
);
814 if (ch
!= '{' && ch
!= '"')
817 BibReadWord(istr
, fieldValue
+1);
819 // If in the table of strings, replace with string from table.
820 wxChar
*s
= (wxChar
*)BibStringTable
.Get(fieldValue
);
823 wxStrcpy(fieldValue
, s
);
827 BibReadValue(istr
, fieldValue
, true, (ch
== _T('"') ? true : false));
829 // Now we can add a field
830 if (StringMatch(recordField
, _T("author"), false, true))
831 bibEntry
->author
= copystring(fieldValue
);
832 else if (StringMatch(recordField
, _T("key"), false, true))
834 else if (StringMatch(recordField
, _T("annotate"), false, true))
836 else if (StringMatch(recordField
, _T("abstract"), false, true))
838 else if (StringMatch(recordField
, _T("edition"), false, true))
840 else if (StringMatch(recordField
, _T("howpublished"), false, true))
842 else if (StringMatch(recordField
, _T("note"), false, true) || StringMatch(recordField
, _T("notes"), false, true))
844 else if (StringMatch(recordField
, _T("series"), false, true))
846 else if (StringMatch(recordField
, _T("type"), false, true))
848 else if (StringMatch(recordField
, _T("keywords"), false, true))
850 else if (StringMatch(recordField
, _T("editor"), false, true) || StringMatch(recordField
, _T("editors"), false, true))
851 bibEntry
->editor
= copystring(fieldValue
);
852 else if (StringMatch(recordField
, _T("title"), false, true))
853 bibEntry
->title
= copystring(fieldValue
);
854 else if (StringMatch(recordField
, _T("booktitle"), false, true))
855 bibEntry
->booktitle
= copystring(fieldValue
);
856 else if (StringMatch(recordField
, _T("journal"), false, true))
857 bibEntry
->journal
= copystring(fieldValue
);
858 else if (StringMatch(recordField
, _T("volume"), false, true))
859 bibEntry
->volume
= copystring(fieldValue
);
860 else if (StringMatch(recordField
, _T("number"), false, true))
861 bibEntry
->number
= copystring(fieldValue
);
862 else if (StringMatch(recordField
, _T("year"), false, true))
863 bibEntry
->year
= copystring(fieldValue
);
864 else if (StringMatch(recordField
, _T("month"), false, true))
865 bibEntry
->month
= copystring(fieldValue
);
866 else if (StringMatch(recordField
, _T("pages"), false, true))
867 bibEntry
->pages
= copystring(fieldValue
);
868 else if (StringMatch(recordField
, _T("publisher"), false, true))
869 bibEntry
->publisher
= copystring(fieldValue
);
870 else if (StringMatch(recordField
, _T("address"), false, true))
871 bibEntry
->address
= copystring(fieldValue
);
872 else if (StringMatch(recordField
, _T("institution"), false, true) || StringMatch(recordField
, _T("school"), false, true))
873 bibEntry
->institution
= copystring(fieldValue
);
874 else if (StringMatch(recordField
, _T("organization"), false, true) || StringMatch(recordField
, _T("organisation"), false, true))
875 bibEntry
->organization
= copystring(fieldValue
);
876 else if (StringMatch(recordField
, _T("comment"), false, true) || StringMatch(recordField
, _T("comments"), false, true))
877 bibEntry
->comment
= copystring(fieldValue
);
878 else if (StringMatch(recordField
, _T("annote"), false, true))
879 bibEntry
->comment
= copystring(fieldValue
);
880 else if (StringMatch(recordField
, _T("chapter"), false, true))
881 bibEntry
->chapter
= copystring(fieldValue
);
884 wxSnprintf(buf
, sizeof(buf
), _T("Unrecognised bib field type %s at line %ld (%s)"), recordField
, BibLine
, filename
);
889 BibList
.Append(recordKey
, bibEntry
);
890 BibEatWhiteSpace(istr
);
896 void OutputBibItem(TexRef
*ref
, BibEntry
*bib
)
900 OnMacro(ltNUMBEREDBIBITEM
, 2, true);
901 OnArgument(ltNUMBEREDBIBITEM
, 1, true);
902 TexOutput(ref
->sectionNumber
);
903 OnArgument(ltNUMBEREDBIBITEM
, 1, false);
904 OnArgument(ltNUMBEREDBIBITEM
, 2, true);
907 OnMacro(ltBF
, 1, true);
908 OnArgument(ltBF
, 1, true);
910 TexOutput(bib
->author
);
911 OnArgument(ltBF
, 1, false);
912 OnMacro(ltBF
, 1, false);
913 if (bib
->author
&& (wxStrlen(bib
->author
) > 0) && (bib
->author
[wxStrlen(bib
->author
) - 1] != '.'))
920 TexOutput(bib
->year
);
925 TexOutput(bib
->month
);
928 if (bib
->year
|| bib
->month
)
931 if (StringMatch(bib
->type
, _T("article"), false, true))
935 TexOutput(bib
->title
);
940 OnMacro(ltIT
, 1, true);
941 OnArgument(ltIT
, 1, true);
942 TexOutput(bib
->journal
);
943 OnArgument(ltIT
, 1, false);
944 OnMacro(ltIT
, 1, false);
949 OnMacro(ltBF
, 1, true);
950 OnArgument(ltBF
, 1, true);
951 TexOutput(bib
->volume
);
952 OnArgument(ltBF
, 1, false);
953 OnMacro(ltBF
, 1, false);
958 TexOutput(bib
->number
);
963 TexOutput(_T(", pages "));
964 TexOutput(bib
->pages
);
968 else if (StringMatch(bib
->type
, _T("book"), false, true) ||
969 StringMatch(bib
->type
, _T("unpublished"), false, true) ||
970 StringMatch(bib
->type
, _T("manual"), false, true) ||
971 StringMatch(bib
->type
, _T("phdthesis"), false, true) ||
972 StringMatch(bib
->type
, _T("mastersthesis"), false, true) ||
973 StringMatch(bib
->type
, _T("misc"), false, true) ||
974 StringMatch(bib
->type
, _T("techreport"), false, true) ||
975 StringMatch(bib
->type
, _T("booklet"), false, true))
977 if (bib
->title
|| bib
->booktitle
)
979 OnMacro(ltIT
, 1, true);
980 OnArgument(ltIT
, 1, true);
981 TexOutput(bib
->title
? bib
->title
: bib
->booktitle
);
983 OnArgument(ltIT
, 1, false);
984 OnMacro(ltIT
, 1, false);
986 if (StringMatch(bib
->type
, _T("phdthesis"), false, true))
987 TexOutput(_T("PhD thesis. "));
988 if (StringMatch(bib
->type
, _T("techreport"), false, true))
989 TexOutput(_T("Technical report. "));
992 TexOutput(_T("Ed. "));
993 TexOutput(bib
->editor
);
996 if (bib
->institution
)
998 TexOutput(bib
->institution
);
1001 if (bib
->organization
)
1003 TexOutput(bib
->organization
);
1004 TexOutput(_T(". "));
1008 TexOutput(bib
->publisher
);
1009 TexOutput(_T(". "));
1013 TexOutput(bib
->address
);
1014 TexOutput(_T(". "));
1017 else if (StringMatch(bib
->type
, _T("inbook"), false, true) ||
1018 StringMatch(bib
->type
, _T("inproceedings"), false, true) ||
1019 StringMatch(bib
->type
, _T("incollection"), false, true) ||
1020 StringMatch(bib
->type
, _T("conference"), false, true))
1024 TexOutput(bib
->title
);
1028 TexOutput(_T(", from "));
1029 OnMacro(ltIT
, 1, true);
1030 OnArgument(ltIT
, 1, true);
1031 TexOutput(bib
->booktitle
);
1033 OnArgument(ltIT
, 1, false);
1034 OnMacro(ltIT
, 1, false);
1038 TexOutput(_T(", ed. "));
1039 TexOutput(bib
->editor
);
1044 TexOutput(bib
->publisher
);
1048 if (bib
->publisher
) TexOutput(_T(", "));
1049 else TexOutput(_T(" "));
1050 TexOutput(bib
->address
);
1052 if (bib
->publisher
|| bib
->address
)
1058 OnMacro(ltBF
, 1, true);
1059 OnArgument(ltBF
, 1, true);
1060 TexOutput(bib
->volume
);
1061 OnArgument(ltBF
, 1, false);
1062 OnMacro(ltBF
, 1, false);
1069 TexOutput(bib
->number
);
1070 TexOutput(_T(")."));
1074 TexOutput(_T(" Number "));
1075 TexOutput(bib
->number
);
1081 TexOutput(_T(" Chap. "));
1082 TexOutput(bib
->chapter
);
1086 if (bib
->chapter
) TexOutput(_T(", pages "));
1087 else TexOutput(_T(" Pages "));
1088 TexOutput(bib
->pages
);
1092 OnArgument(ltNUMBEREDBIBITEM
, 2, false);
1093 OnMacro(ltNUMBEREDBIBITEM
, 2, false);
1096 void OutputBib(void)
1098 // Write the heading
1099 ForceTopicName(_T("bibliography"));
1100 FakeCurrentSection(ReferencesNameString
);
1101 ForceTopicName(NULL
);
1103 OnMacro(ltPAR
, 0, true);
1104 OnMacro(ltPAR
, 0, false);
1106 if ((convertMode
== TEX_RTF
) && !winHelp
)
1108 OnMacro(ltPAR
, 0, true);
1109 OnMacro(ltPAR
, 0, false);
1112 wxStringListNode
*node
= CitationList
.GetFirst();
1115 wxChar
*citeKey
= (wxChar
*)node
->GetData();
1116 // wxNode *texNode = TexReferences.Find(citeKey);
1117 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
1118 wxNode
*bibNode
= BibList
.Find(citeKey
);
1121 BibEntry
*entry
= (BibEntry
*)bibNode
->GetData();
1122 OutputBibItem(ref
, entry
);
1124 node
= node
->GetNext();
1128 static int citeCount
= 1;
1130 void ResolveBibReferences(void)
1132 if (CitationList
.GetCount() > 0)
1133 OnInform(_T("Resolving bibliographic references..."));
1137 wxStringListNode
*node
= CitationList
.GetFirst();
1141 wxChar
*citeKey
= (wxChar
*)node
->GetData();
1142 // wxNode *texNode = TexReferences.Find(citeKey);
1143 TexRef
*ref
= (TexRef
*)TexReferences
.Get(citeKey
);
1144 wxNode
*bibNode
= BibList
.Find(citeKey
);
1148 //BibEntry *entry = (BibEntry *)bibNode->GetData();
1149 if (ref
->sectionNumber
) delete[] ref
->sectionNumber
;
1150 wxSnprintf(buf
, sizeof(buf
), _T("[%d]"), citeCount
);
1151 ref
->sectionNumber
= copystring(buf
);
1156 wxSnprintf(buf
, sizeof(buf
), _T("Warning: bib ref %s not resolved."), citeKey
);
1159 node
= node
->GetNext();
1163 // Remember we need to resolve this citation
1164 void AddCitation(const wxChar
*citeKey
)
1166 if (!CitationList
.Member(citeKey
))
1167 CitationList
.Add(citeKey
);
1169 if (!TexReferences
.Get(citeKey
))
1171 TexReferences
.Put(citeKey
, new TexRef(citeKey
, _T("??"), NULL
));
1175 TexRef
*FindReference(const wxChar
*key
)
1177 return (TexRef
*)TexReferences
.Get(key
);
1181 * Custom macro stuff
1185 bool StringTobool(const wxString
& val
)
1190 if (up
.IsSameAs(_T("YES")) ||
1191 up
.IsSameAs(_T("TRUE")) ||
1192 up
.IsSameAs(_T("ON")) ||
1193 up
.IsSameAs(_T("OK")) |
1194 up
.IsSameAs(_T("1")))
1200 void RegisterIntSetting (const wxString
& s
, int *number
)
1210 // Define a variable value from the .ini file
1211 wxChar
*RegisterSetting(const wxString
& settingName
, const wxString
& settingValue
, bool interactive
)
1213 wxString
settingValueStr( settingValue
);
1215 static wxChar errorCode
[100];
1216 wxStrcpy(errorCode
, _T("OK"));
1217 if (StringMatch(settingName
, _T("chapterName"), false, true))
1219 delete[] ChapterNameString
;
1220 ChapterNameString
= copystring(settingValue
);
1222 else if (StringMatch(settingName
, _T("sectionName"), false, true))
1224 delete[] SectionNameString
;
1225 SectionNameString
= copystring(settingValue
);
1227 else if (StringMatch(settingName
, _T("subsectionName"), false, true))
1229 delete[] SubsectionNameString
;
1230 SubsectionNameString
= copystring(settingValue
);
1232 else if (StringMatch(settingName
, _T("subsubsectionName"), false, true))
1234 delete[] SubsubsectionNameString
;
1235 SubsubsectionNameString
= copystring(settingValue
);
1237 else if (StringMatch(settingName
, _T("indexName"), false, true))
1239 delete[] IndexNameString
;
1240 IndexNameString
= copystring(settingValue
);
1242 else if (StringMatch(settingName
, _T("contentsName"), false, true))
1244 delete[] ContentsNameString
;
1245 ContentsNameString
= copystring(settingValue
);
1247 else if (StringMatch(settingName
, _T("glossaryName"), false, true))
1249 delete[] GlossaryNameString
;
1250 GlossaryNameString
= copystring(settingValue
);
1252 else if (StringMatch(settingName
, _T("referencesName"), false, true))
1254 delete[] ReferencesNameString
;
1255 ReferencesNameString
= copystring(settingValue
);
1257 else if (StringMatch(settingName
, _T("tablesName"), false, true))
1259 delete[] TablesNameString
;
1260 TablesNameString
= copystring(settingValue
);
1262 else if (StringMatch(settingName
, _T("figuresName"), false, true))
1264 delete[] FiguresNameString
;
1265 FiguresNameString
= copystring(settingValue
);
1267 else if (StringMatch(settingName
, _T("tableName"), false, true))
1269 delete[] TableNameString
;
1270 TableNameString
= copystring(settingValue
);
1272 else if (StringMatch(settingName
, _T("figureName"), false, true))
1274 delete[] FigureNameString
;
1275 FigureNameString
= copystring(settingValue
);
1277 else if (StringMatch(settingName
, _T("abstractName"), false, true))
1279 delete[] AbstractNameString
;
1280 AbstractNameString
= copystring(settingValue
);
1282 else if (StringMatch(settingName
, _T("chapterFontSize"), false, true))
1283 RegisterIntSetting(settingValueStr
, &chapterFont
);
1284 else if (StringMatch(settingName
, _T("sectionFontSize"), false, true))
1285 RegisterIntSetting(settingValueStr
, §ionFont
);
1286 else if (StringMatch(settingName
, _T("subsectionFontSize"), false, true))
1287 RegisterIntSetting(settingValueStr
, &subsectionFont
);
1288 else if (StringMatch(settingName
, _T("titleFontSize"), false, true))
1289 RegisterIntSetting(settingValueStr
, &titleFont
);
1290 else if (StringMatch(settingName
, _T("authorFontSize"), false, true))
1291 RegisterIntSetting(settingValueStr
, &authorFont
);
1292 else if (StringMatch(settingName
, _T("ignoreInput"), false, true))
1293 IgnorableInputFiles
.Add(wxFileNameFromPath(settingValue
));
1294 else if (StringMatch(settingName
, _T("mirrorMargins"), false, true))
1295 mirrorMargins
= StringTobool(settingValue
);
1296 else if (StringMatch(settingName
, _T("runTwice"), false, true))
1297 runTwice
= StringTobool(settingValue
);
1298 else if (StringMatch(settingName
, _T("isInteractive"), false, true))
1299 isInteractive
= StringTobool(settingValue
);
1300 else if (StringMatch(settingName
, _T("headerRule"), false, true))
1301 headerRule
= StringTobool(settingValue
);
1302 else if (StringMatch(settingName
, _T("footerRule"), false, true))
1303 footerRule
= StringTobool(settingValue
);
1304 else if (StringMatch(settingName
, _T("combineSubSections"), false, true))
1305 combineSubSections
= StringTobool(settingValue
);
1306 else if (StringMatch(settingName
, _T("listLabelIndent"), false, true))
1307 RegisterIntSetting(settingValueStr
, &labelIndentTab
);
1308 else if (StringMatch(settingName
, _T("listItemIndent"), false, true))
1309 RegisterIntSetting(settingValueStr
, &itemIndentTab
);
1310 else if (StringMatch(settingName
, _T("useUpButton"), false, true))
1311 useUpButton
= StringTobool(settingValue
);
1312 else if (StringMatch(settingName
, _T("useHeadingStyles"), false, true))
1313 useHeadingStyles
= StringTobool(settingValue
);
1314 else if (StringMatch(settingName
, _T("useWord"), false, true))
1315 useWord
= StringTobool(settingValue
);
1316 else if (StringMatch(settingName
, _T("contentsDepth"), false, true))
1317 RegisterIntSetting(settingValueStr
, &contentsDepth
);
1318 else if (StringMatch(settingName
, _T("generateHPJ"), false, true))
1319 generateHPJ
= StringTobool(settingValue
);
1320 else if (StringMatch(settingName
, _T("truncateFilenames"), false, true))
1321 truncateFilenames
= StringTobool(settingValue
);
1322 else if (StringMatch(settingName
, _T("winHelpVersion"), false, true))
1323 RegisterIntSetting(settingValueStr
, &winHelpVersion
);
1324 else if (StringMatch(settingName
, _T("winHelpContents"), false, true))
1325 winHelpContents
= StringTobool(settingValue
);
1326 else if (StringMatch(settingName
, _T("htmlIndex"), false, true))
1327 htmlIndex
= StringTobool(settingValue
);
1328 else if (StringMatch(settingName
, _T("htmlWorkshopFiles"), false, true))
1329 htmlWorkshopFiles
= StringTobool(settingValue
);
1330 else if (StringMatch(settingName
, _T("htmlFrameContents"), false, true))
1331 htmlFrameContents
= StringTobool(settingValue
);
1332 else if (StringMatch(settingName
, _T("htmlStylesheet"), false, true))
1335 delete[] htmlStylesheet
;
1336 htmlStylesheet
= copystring(settingValue
);
1338 else if (StringMatch(settingName
, _T("upperCaseNames"), false, true))
1339 upperCaseNames
= StringTobool(settingValue
);
1340 else if (StringMatch(settingName
, _T("ignoreBadRefs"), false, true))
1341 ignoreBadRefs
= StringTobool(settingValue
);
1342 else if (StringMatch(settingName
, _T("htmlFaceName"), false, true))
1344 delete[] htmlFaceName
;
1345 htmlFaceName
= copystring(settingValue
);
1347 else if (StringMatch(settingName
, _T("winHelpTitle"), false, true))
1350 delete[] winHelpTitle
;
1351 winHelpTitle
= copystring(settingValue
);
1353 else if (StringMatch(settingName
, _T("indexSubsections"), false, true))
1354 indexSubsections
= StringTobool(settingValue
);
1355 else if (StringMatch(settingName
, _T("compatibility"), false, true))
1356 compatibilityMode
= StringTobool(settingValue
);
1357 else if (StringMatch(settingName
, _T("defaultColumnWidth"), false, true))
1359 RegisterIntSetting(settingValueStr
, &defaultTableColumnWidth
);
1360 defaultTableColumnWidth
= 20*defaultTableColumnWidth
;
1362 else if (StringMatch(settingName
, _T("bitmapMethod"), false, true))
1364 if ((wxStrcmp(settingValue
, _T("includepicture")) != 0) && (wxStrcmp(settingValue
, _T("hex")) != 0) &&
1365 (wxStrcmp(settingValue
, _T("import")) != 0))
1368 OnError(_T("Unknown bitmapMethod"));
1369 wxStrcpy(errorCode
, _T("Unknown bitmapMethod"));
1373 delete[] bitmapMethod
;
1374 bitmapMethod
= copystring(settingValue
);
1377 else if (StringMatch(settingName
, _T("htmlBrowseButtons"), false, true))
1379 if (wxStrcmp(settingValue
, _T("none")) == 0)
1380 htmlBrowseButtons
= HTML_BUTTONS_NONE
;
1381 else if (wxStrcmp(settingValue
, _T("bitmap")) == 0)
1382 htmlBrowseButtons
= HTML_BUTTONS_BITMAP
;
1383 else if (wxStrcmp(settingValue
, _T("text")) == 0)
1384 htmlBrowseButtons
= HTML_BUTTONS_TEXT
;
1388 OnInform(_T("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text."));
1389 wxStrcpy(errorCode
, _T("Initialisation file error: htmlBrowseButtons must be one of none, bitmap, or text."));
1392 else if (StringMatch(settingName
, _T("backgroundImage"), false, true))
1394 backgroundImageString
= copystring(settingValue
);
1396 else if (StringMatch(settingName
, _T("backgroundColour"), false, true))
1398 delete[] backgroundColourString
;
1399 backgroundColourString
= copystring(settingValue
);
1401 else if (StringMatch(settingName
, _T("textColour"), false, true))
1403 textColourString
= copystring(settingValue
);
1405 else if (StringMatch(settingName
, _T("linkColour"), false, true))
1407 linkColourString
= copystring(settingValue
);
1409 else if (StringMatch(settingName
, _T("followedLinkColour"), false, true))
1411 followedLinkColourString
= copystring(settingValue
);
1413 else if (StringMatch(settingName
, _T("conversionMode"), false, true))
1415 if (StringMatch(settingValue
, _T("RTF"), false, true))
1417 winHelp
= false; convertMode
= TEX_RTF
;
1419 else if (StringMatch(settingValue
, _T("WinHelp"), false, true))
1421 winHelp
= true; convertMode
= TEX_RTF
;
1423 else if (StringMatch(settingValue
, _T("XLP"), false, true) ||
1424 StringMatch(settingValue
, _T("wxHelp"), false, true))
1426 convertMode
= TEX_XLP
;
1428 else if (StringMatch(settingValue
, _T("HTML"), false, true))
1430 convertMode
= TEX_HTML
;
1435 OnInform(_T("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML."));
1436 wxStrcpy(errorCode
, _T("Initialisation file error: conversionMode must be one of\nRTF, WinHelp, XLP (or wxHelp), HTML."));
1439 else if (StringMatch(settingName
, _T("documentFontSize"), false, true))
1442 RegisterIntSetting(settingValueStr
, &n
);
1443 if (n
== 10 || n
== 11 || n
== 12)
1448 wxSnprintf(buf
, sizeof(buf
), _T("Initialisation file error: nonstandard document font size %d."), n
);
1451 wxStrcpy(errorCode
, buf
);
1457 wxSnprintf(buf
, sizeof(buf
), _T("Initialisation file error: unrecognised setting %s."), settingName
.c_str());
1460 wxStrcpy(errorCode
, buf
);
1465 bool ReadCustomMacros(const wxString
& filename
)
1467 if (!wxFileExists(filename
))
1470 wxFileInputStream
input( filename
);
1471 if(!input
.Ok()) return false;
1472 wxTextInputStream
ini( input
);
1474 CustomMacroList
.Clear();
1476 while (!input
.Eof())
1478 wxString line
= ini
.ReadLine();
1479 BibEatWhiteSpace(line
);
1480 if (line
.empty()) continue;
1482 if (line
[0] != _T('\\')) // Not a macro definition, so must be NAME=VALUE
1484 wxString settingName
= BibReadWord(line
);
1485 BibEatWhiteSpace(line
);
1486 if (line
.empty() || line
[0] != _T('='))
1488 OnError(_T("Expected = following name: malformed tex2rtf.ini file."));
1493 line
= line
.substr(1);
1494 BibEatWhiteSpace(line
);
1495 wxString settingValue
= BibReadToEOL(line
);
1496 RegisterSetting(settingName
, settingValue
);
1501 line
= line
.substr(1);
1502 wxString macroName
= BibReadWord(line
);
1503 BibEatWhiteSpace(line
);
1504 if (line
[0] != _T('['))
1506 OnError(_T("Expected [ followed by number of arguments: malformed tex2rtf.ini file."));
1509 line
= line
.substr(1);
1510 wxString noAargStr
= line
.BeforeFirst(_T(']'));
1511 line
= line
.AfterFirst(_T(']'));
1513 if (!noAargStr
.ToLong(&noArgs
) || line
.empty())
1515 OnError(_T("Expected ] following number of arguments: malformed tex2rtf.ini file."));
1518 BibEatWhiteSpace(line
);
1519 if (line
[0] != _T('{'))
1521 OnError(_T("Expected { followed by macro body: malformed tex2rtf.ini file."));
1525 CustomMacro
*macro
= new CustomMacro(macroName
.c_str(), noArgs
, NULL
);
1526 wxString macroBody
= BibReadValue(line
, false, false); // Don't ignore extra braces
1527 if (!macroBody
.empty())
1528 macro
->macroBody
= copystring(macroBody
.c_str());
1530 BibEatWhiteSpace(line
);
1531 CustomMacroList
.Append(macroName
, macro
);
1532 AddMacroDef(ltCUSTOM_MACRO
, macroName
.c_str(), noArgs
);
1537 wxSnprintf(mbuf
, sizeof(mbuf
), _T("Read initialization file %s."), filename
.c_str());
1542 CustomMacro
*FindCustomMacro(wxChar
*name
)
1544 wxNode
*node
= CustomMacroList
.Find(name
);
1547 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
1553 // Display custom macros
1554 void ShowCustomMacros(void)
1556 wxNode
*node
= CustomMacroList
.GetFirst();
1559 OnInform(_T("No custom macros loaded.\n"));
1566 CustomMacro
*macro
= (CustomMacro
*)node
->GetData();
1567 wxSnprintf(buf
, sizeof(buf
), _T("\\%s[%d]\n {%s}"), macro
->macroName
, macro
->noArgs
,
1568 macro
->macroBody
? macro
->macroBody
: _T(""));
1570 node
= node
->GetNext();
1574 // Parse a string into several comma-separated fields
1575 wxChar
*ParseMultifieldString(wxChar
*allFields
, int *pos
)
1577 static wxChar buffer
[300];
1579 int fieldIndex
= *pos
;
1580 int len
= wxStrlen(allFields
);
1582 bool keepGoing
= true;
1583 while ((fieldIndex
<= len
) && keepGoing
)
1585 if (allFields
[fieldIndex
] == _T(' '))
1590 else if (allFields
[fieldIndex
] == _T(','))
1592 *pos
= fieldIndex
+ 1;
1595 else if (allFields
[fieldIndex
] == 0)
1597 *pos
= fieldIndex
+ 1;
1602 buffer
[i
] = allFields
[fieldIndex
];
1608 if (oldPos
== (*pos
))
1622 ColourTableEntry::ColourTableEntry(const wxChar
*theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1624 name
= copystring(theName
);
1630 ColourTableEntry::~ColourTableEntry(void)
1635 void AddColour(const wxChar
*theName
, unsigned int r
, unsigned int g
, unsigned int b
)
1637 wxNode
*node
= ColourTable
.Find(theName
);
1640 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->GetData();
1641 if (entry
->red
== r
|| entry
->green
== g
|| entry
->blue
== b
)
1649 ColourTableEntry
*entry
= new ColourTableEntry(theName
, r
, g
, b
);
1650 ColourTable
.Append(theName
, entry
);
1653 int FindColourPosition(wxChar
*theName
)
1656 wxNode
*node
= ColourTable
.GetFirst();
1659 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->GetData();
1660 if (wxStrcmp(theName
, entry
->name
) == 0)
1663 node
= node
->GetNext();
1668 // Converts e.g. "red" -> "#FF0000"
1669 extern void DecToHex(int, wxChar
*);
1670 bool FindColourHTMLString(wxChar
*theName
, wxChar
*buf
)
1672 wxNode
*node
= ColourTable
.GetFirst();
1675 ColourTableEntry
*entry
= (ColourTableEntry
*)node
->GetData();
1676 if (wxStrcmp(theName
, entry
->name
) == 0)
1678 wxStrcpy(buf
, _T("#"));
1681 DecToHex(entry
->red
, buf2
);
1682 wxStrcat(buf
, buf2
);
1683 DecToHex(entry
->green
, buf2
);
1684 wxStrcat(buf
, buf2
);
1685 DecToHex(entry
->blue
, buf2
);
1686 wxStrcat(buf
, buf2
);
1690 node
= node
->GetNext();
1696 void InitialiseColourTable(void)
1698 // \\red0\\green0\\blue0;
1699 AddColour(_T("black"), 0,0,0);
1701 // \\red0\\green0\\blue255;\\red0\\green255\\blue255;\n");
1702 AddColour(_T("cyan"), 0,255,255);
1704 // \\red0\\green255\\blue0;
1705 AddColour(_T("green"), 0,255,0);
1707 // \\red255\\green0\\blue255;
1708 AddColour(_T("magenta"), 255,0,255);
1710 // \\red255\\green0\\blue0;
1711 AddColour(_T("red"), 255,0,0);
1713 // \\red255\\green255\\blue0;
1714 AddColour(_T("yellow"), 255,255,0);
1716 // \\red255\\green255\\blue255;}");
1717 AddColour(_T("white"), 255,255,255);
1721 * The purpose of this is to reduce the number of times wxYield is
1722 * called, since under Windows this can slow things down.
1725 void Tex2RTFYield(bool force
)
1728 static int yieldCount
= 0;
1735 if (yieldCount
== 0)
1747 // In both RTF generation and HTML generation for wxHelp version 2,
1748 // we need to associate \indexed keywords with the current filename/topics.
1750 // Hash table for lists of keywords for topics (WinHelp).
1751 wxHashTable
TopicTable(wxKEY_STRING
);
1752 void AddKeyWordForTopic(wxChar
*topic
, wxChar
*entry
, wxChar
*filename
)
1754 TexTopic
*texTopic
= (TexTopic
*)TopicTable
.Get(topic
);
1757 texTopic
= new TexTopic(filename
);
1758 texTopic
->keywords
= new wxStringList
;
1759 TopicTable
.Put(topic
, texTopic
);
1762 if (!texTopic
->keywords
->Member(entry
))
1763 texTopic
->keywords
->Add(entry
);
1766 void ClearKeyWordTable(void)
1768 TopicTable
.BeginFind();
1769 wxHashTable::Node
*node
= TopicTable
.Next();
1772 TexTopic
*texTopic
= (TexTopic
*)node
->GetData();
1774 node
= TopicTable
.Next();
1781 * TexTopic structure
1784 TexTopic::TexTopic(wxChar
*f
)
1787 filename
= copystring(f
);
1790 hasChildren
= false;
1794 TexTopic::~TexTopic(void)
1802 // Convert case, according to upperCaseNames setting.
1803 wxChar
*ConvertCase(wxChar
*s
)
1805 static wxChar buf
[256];
1806 int len
= wxStrlen(s
);
1809 for (i
= 0; i
< len
; i
++)
1810 buf
[i
] = (wxChar
)wxToupper(s
[i
]);
1812 for (i
= 0; i
< len
; i
++)
1813 buf
[i
] = (wxChar
)wxTolower(s
[i
]);
1818 // if substring is true, search for str1 in str2
1819 bool StringMatch(const wxChar
*str1
, const wxChar
*str2
, bool subString
,
1824 wxString
Sstr1(str1
);
1825 wxString
Sstr2(str2
);
1831 return Sstr2
.Index(Sstr1
) != (size_t)wxNOT_FOUND
;
1834 return exact
? wxString(str2
).Cmp(str1
) == 0 :
1835 wxString(str2
).CmpNoCase(str1
) == 0;