~s-cecilio/lenmus/trunk

« back to all changes in this revision

Viewing changes to langtool/src/ebook_processor.cpp

  • Committer: cecilios
  • Date: 2006-12-11 17:40:31 UTC
  • Revision ID: svn-v4:2587a929-2f0e-0410-ae78-fe6f687d5efe:trunk:176

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
#include "ebook_processor.h"
43
43
#include "wx/xml2.h"            // include libxml2 wrapper definitions
44
44
#include "wx/dtd.h"                             // include libxml2 wrapper definitions
 
45
#include "Paths.h"
45
46
 
46
47
 
47
48
#define ltNO_INDENT false
53
54
    eTRANSLATE = 8
54
55
};
55
56
 
 
57
static const wxString m_sFooter1 = 
 
58
    _T("Send your comments and sugesstions to LenMus team (www.lenmus.org)");
 
59
static const wxString m_sFooter2 = 
 
60
        _T("Licensed under the terms of the GNU Free Documentation License (see copyrights page for details.)");
 
61
 
 
62
 
56
63
lmEbookProcessor::lmEbookProcessor()
57
64
{
58
65
    m_pTocFile = (wxFile*) NULL;
59
66
    m_pHtmlFile = (wxFile*) NULL;
60
 
    m_pPoFile = (wxFile*)NULL;
 
67
    m_pLangFile = (wxFile*)NULL;
61
68
    m_pLmbFile = (wxTextOutputStream*)NULL;
62
69
    m_pZipFile = (wxZipOutputStream*)NULL;
63
70
 
70
77
{
71
78
    if (m_pTocFile) delete m_pTocFile;
72
79
    if (m_pHtmlFile) delete m_pHtmlFile;
73
 
    if (m_pPoFile) delete m_pPoFile;
 
80
    if (m_pLangFile) delete m_pLangFile;
74
81
    if (m_pLmbFile) delete m_pLmbFile;
75
82
    if (m_pZipFile) delete m_pZipFile;
76
83
 
77
84
}
78
85
 
79
 
bool lmEbookProcessor::GenerateLMB(wxString sFilename, int nOptions)
 
86
bool lmEbookProcessor::GenerateLMB(wxString sFilename, wxString sLangCode, int nOptions)
80
87
{
81
88
    // returns false if error
 
89
    // PO and lang.cpp are created in sDestName (langtool\locale\)
 
90
    // book.lmb is created in sDestName (lenmus\books\xx\)
 
91
    // book.toc is temporarily created in sDestName (lenmus\books\xx\)
82
92
 
83
93
    // Prepare for a new XML file processing
84
94
    m_fProcessingBookinfo = false;
85
 
    m_fOnlyPoFile = nOptions & lmPO_FILE;
 
95
    m_fOnlyLangFile = nOptions & lmLANG_FILE;
 
96
    m_fGenerateLmb = !m_fOnlyLangFile;
 
97
 
86
98
 
87
99
    // load the XML file as tree of nodes
88
100
    wxXml2Document oDoc;
107
119
    //DumpXMLTree(oRoot);   //DBG
108
120
    CreateLinksTable(oRoot);
109
121
 
110
 
    // Prepare PO file
111
 
    if (m_fOnlyPoFile) {
112
 
        if (!StartPoFile( sFilename )) {
113
 
            wxLogMessage(_T("Error: PO file can not be created"));
 
122
    // Prepare Lang file
 
123
    if (m_fOnlyLangFile) {
 
124
        if (!StartLangFile( sFilename )) {
 
125
            wxLogMessage(_T("Error: Lang file can not be created"));
114
126
            oRoot.DestroyIfUnlinked();
115
127
            oDoc.DestroyIfUnlinked();
116
128
            return false;        //error
127
139
 
128
140
    // prepare de LMB file
129
141
    if (m_fGenerateLmb) {
130
 
        if (!StartLmbFile( sFilename )) {
 
142
        if (!StartLmbFile(sFilename, sLangCode)) {
131
143
            wxLogMessage(_T("Error: LMB file can not be created"));
132
144
            oRoot.DestroyIfUnlinked();
133
145
            oDoc.DestroyIfUnlinked();
292
304
        // are for filtering ou these lines
293
305
        wxString tmp = sContent.Trim();
294
306
        if (!tmp.IsEmpty()) {
295
 
            if (nWriteOptions & eTOC) WriteToToc(sContent, ltNO_INDENT);        //text not indented
296
 
            if (nWriteOptions & eHTML) WriteToHtml(sContent);
297
 
            if (m_fOnlyPoFile && (nWriteOptions & eTRANSLATE)) WriteToPo(sContent);
 
307
            wxString sTrans = wxGetTranslation(sContent);
 
308
            if (nWriteOptions & eTOC) WriteToToc(sTrans, ltNO_INDENT);        //text not indented
 
309
            if (nWriteOptions & eHTML) WriteToHtml(sTrans);
 
310
            if (m_fOnlyLangFile && (nWriteOptions & eTRANSLATE)) WriteToLang(sContent);
298
311
            //if (fIdx) WriteToIdx(sContent);
299
312
            if (pText) *pText += sContent;
300
313
        }
808
821
            _T("<table width='100%' cellpadding='0' cellspacing='0'>\n")
809
822
            _T("<tr><td bgcolor='#7f8adc' align='left'>\n")
810
823
            _T("        <font size='-1' color='#ffffff'>&nbsp;&nbsp;") );
811
 
        WriteToHtml(m_sBookTitle);
 
824
        WriteToHtml( wxGetTranslation(m_sBookTitle) );
812
825
        WriteToHtml(
813
826
            _T("</font></td>\n")
814
827
            _T("<tr><td bgcolor='#7f8adc' align='right'><br />\n")
818
831
            WriteToHtml( sTitleNum );
819
832
        }
820
833
        else
821
 
            WriteToHtml(sTitleNum + m_sChapterTitle);
 
834
            WriteToHtml(sTitleNum + wxGetTranslation(m_sChapterTitle) );
822
835
        WriteToHtml(
823
836
            _T("&nbsp;</font></b><br /></td>\n")
824
837
            _T("<tr><td bgcolor='#ff8800'><img src='ebook_line.png'></td></tr>\n")
845
858
{
846
859
    wxString sTitleNum = wxEmptyString;
847
860
    if (m_nTitleLevel >= 0)
848
 
        sTitleNum += wxString::Format(_T("$d"), m_nNumTitle[0] );
 
861
        sTitleNum += wxString::Format(_T("%d"), m_nNumTitle[0] );
849
862
    for (int i=1; i <= m_nTitleLevel; i++) {
850
 
        sTitleNum += wxString::Format(_T(".$d"), m_nNumTitle[i] );
 
863
        sTitleNum += wxString::Format(_T(".%d"), m_nNumTitle[i] );
851
864
    }
852
865
    if (sTitleNum != wxEmptyString) sTitleNum += _T(" ");
853
866
    
1001
1014
        _T("<table width='100%' cellpadding='0' cellspacing='0'>\n")
1002
1015
        _T("<tr><td bgcolor='#ff8800'><img src='ebook_line.png'></td></tr>\n")
1003
1016
        _T("<tr><td bgcolor='#7f8adc' align='center'>\n")
1004
 
            _T("    <font size='-1' color='ffffff'><br /><br />\n")
1005
 
            _T("Send your comments and sugesstions to LenMus team (www.lenmus.org)")
1006
 
            _T("<br />\n")
1007
 
        _T("Licensed under the terms of the GNU Free Documentation License (see copyrights page for details.)")
 
1017
            _T("    <font size='-1' color='ffffff'><br /><br />\n") + m_sFooter1 +
 
1018
            _T("<br />\n") + m_sFooter2 +
1008
1019
        _T("<br />\n")
1009
1020
            _T("    </font>\n")
1010
1021
        _T("</td></tr>\n")
1044
1055
//------------------------------------------------------------------------------------
1045
1056
 
1046
1057
 
1047
 
bool lmEbookProcessor::StartLmbFile(wxString sFilename)
 
1058
bool lmEbookProcessor::StartLmbFile(wxString sFilename, wxString sLangCode)
1048
1059
{
1049
1060
    // returns true if success
1050
1061
 
1051
1062
    wxFileName oFNP( sFilename );
1052
 
    oFNP.SetExt(_T("lmb"));
1053
 
    m_pZipOutFile = new wxFFileOutputStream( oFNP.GetFullName() );
 
1063
    wxFileName oFDest( g_pPaths->GetBooksRootPath() );
 
1064
    oFDest.AppendDir(sLangCode);
 
1065
    oFDest.SetName( oFNP.GetName() );
 
1066
    oFDest.SetExt(_T("lmb"));
 
1067
    m_pZipOutFile = new wxFFileOutputStream( oFDest.GetFullPath() );
1054
1068
    //wxFFileOutputStream out( oFNP.GetFullName() );
1055
 
    //if (!m_pPoFile->IsOpened()) {
 
1069
    //if (!m_pLangFile->IsOpened()) {
1056
1070
    //    wxLogMessage(_T("Error: File %s can not be created"), oFNP.GetFullName());
1057
 
    //    m_pPoFile = (wxFile*)NULL;
 
1071
    //    m_pLangFile = (wxFile*)NULL;
1058
1072
    //    return false;        //error
1059
1073
    //}
1060
1074
    m_pZipFile = new wxZipOutputStream(*m_pZipOutFile);
1096
1110
 
1097
1111
 
1098
1112
//------------------------------------------------------------------------------------
1099
 
// PO file management
 
1113
// Lang (.cpp) file management
1100
1114
//------------------------------------------------------------------------------------
1101
1115
 
1102
1116
 
1103
 
bool lmEbookProcessor::StartPoFile(wxString sFilename)
1104
 
{
1105
 
    // returns true if success
1106
 
 
1107
 
    wxFileName oFNP( sFilename );
1108
 
    oFNP.SetExt(_T("po"));
1109
 
    m_pPoFile = new wxFile(oFNP.GetFullName(), wxFile::write);
1110
 
    if (!m_pPoFile->IsOpened()) {
1111
 
        wxLogMessage(_T("Error: File %s can not be created"), oFNP.GetFullName());
1112
 
        m_pPoFile = (wxFile*)NULL;
 
1117
bool lmEbookProcessor::StartLangFile(wxString sFilename)
 
1118
{
 
1119
    // returns true if success
 
1120
 
 
1121
    wxFileName oFNP( sFilename );
 
1122
    wxFileName oFDest( g_pPaths->GetLocalePath() );
 
1123
    oFDest.AppendDir(_T("src"));
 
1124
    ::wxSetWorkingDirectory(  oFDest.GetPath() );
 
1125
    oFDest.AppendDir( oFNP.GetName() );
 
1126
    //if dir does not exist, create it
 
1127
    bool fError = ::wxMkDir( oFNP.GetName() );
 
1128
 
 
1129
    oFDest.SetName( oFNP.GetName() );
 
1130
    oFDest.SetExt(_T("cpp"));
 
1131
    m_pLangFile = new wxFile(oFDest.GetFullPath(), wxFile::write);
 
1132
    if (!m_pLangFile->IsOpened()) {
 
1133
        wxLogMessage(_T("Error: File %s can not be created"), oFDest.GetFullPath());
 
1134
        m_pLangFile = (wxFile*)NULL;
 
1135
        return false;        //error
 
1136
    }
 
1137
 
 
1138
    m_pLangFile->Write(_T("wxString sTxt;\n"));
 
1139
 
 
1140
    // add texts included by Langtool (footers, headers, etc.)
 
1141
    WriteToLang( m_sFooter1 );
 
1142
    WriteToLang( m_sFooter2 );
 
1143
 
 
1144
    return true;
 
1145
 
 
1146
}
 
1147
 
 
1148
void lmEbookProcessor::WriteToLang(wxString sText)
 
1149
{
 
1150
    //add text to Lang file
 
1151
 
 
1152
    if (!m_pLangFile || sText == _T("")) return;
 
1153
 
 
1154
    //change /n by //n
 
1155
    wxString sContent = sText;
 
1156
    sContent.Replace(_T("\n"), _T("\\n"));
 
1157
    m_pLangFile->Write(_T("sTxt = _(\""));
 
1158
    m_pLangFile->Write(sContent + _T("\");\n"));
 
1159
 
 
1160
}
 
1161
 
 
1162
bool lmEbookProcessor::CreatePoFile(wxString& sFilename, wxString& sCharSet,
 
1163
                                    wxString& sLangName, wxString& sLangCode)
 
1164
{
 
1165
    // returns true if success
 
1166
 
 
1167
    wxFileName oFNP( sFilename );
 
1168
    wxFileName oFDest( g_pPaths->GetLocalePath() );
 
1169
    oFDest.AppendDir(sLangCode);
 
1170
    oFDest.SetName( oFNP.GetName() );
 
1171
    oFDest.SetExt(_T("po"));
 
1172
    wxFile oFile(oFDest.GetFullPath(), wxFile::write);
 
1173
    if (!m_pLangFile->IsOpened()) {
 
1174
        wxLogMessage(_T("Error: File %s can not be created"), oFDest.GetFullPath());
 
1175
        m_pLangFile = (wxFile*)NULL;
1113
1176
        return false;        //error
1114
1177
    }
1115
1178
 
1118
1181
    wxString sHeader = sNil +
1119
1182
        _T("msgid \"\"\n") 
1120
1183
        _T("msgstr \"\"\n")
1121
 
        _T("\"Project-Id-Version: LenMus 3.3\\n\"\n")
 
1184
        _T("\"Project-Id-Version: LenMus 3.4\\n\"\n")
1122
1185
        _T("\"POT-Creation-Date: \\n\"\n")
1123
1186
        _T("\"PO-Revision-Date: 2006-08-25 12:19+0100\\n\"\n")
1124
1187
        _T("\"Last-Translator: \\n\"\n")
1125
1188
        _T("\"Language-Team:  <cecilios@gmail.com>\\n\"\n")
1126
1189
        _T("\"MIME-Version: 1.0\\n\"\n")
1127
 
        _T("\"Content-Type: text/plain; charset=utf-8\\n\"\n")
 
1190
        _T("\"Content-Type: text/plain; charset=") + sCharSet + _T("\\n\"\n")
1128
1191
        _T("\"Content-Transfer-Encoding: 8bit\\n\"\n")
1129
 
        _T("\"X-Poedit-Language: English\\n\"\n")
1130
 
        _T("\"X-Poedit-SourceCharset: iso-8859-1\\n\"\n\n");
1131
 
 
1132
 
    m_pPoFile->Write(sHeader);
 
1192
        _T("\"X-Poedit-Language: ") + sLangName + _T("\\n\"\n")
 
1193
        _T("\"X-Poedit-SourceCharset: iso-8859-1\\n\"\n")
 
1194
        _T("\"X-Poedit-Basepath: c:\\usr\\desarrollo_wx\\lenmus\\langtool\\locale\\src\\n\"\n")
 
1195
        _T("\"X-Poedit-SearchPath-0: ") + oFNP.GetName() + _T("\\n\"\n\n");
 
1196
 
 
1197
 
 
1198
    oFile.Write(sHeader);
 
1199
    oFile.Close();
1133
1200
    return true;
1134
1201
 
1135
1202
}
1136
1203
 
1137
 
void lmEbookProcessor::WriteToPo(wxString& sText)
1138
 
{
1139
 
    //add text to PO file
1140
 
 
1141
 
    if (!m_pPoFile || sText == _T("")) return;
1142
 
 
1143
 
    //change /n by //n
1144
 
    wxString sContent = sText;
1145
 
    sContent.Replace(_T("\n"), _T("\\n"));
1146
 
    m_pPoFile->Write(_T("msgid \""));
1147
 
    m_pPoFile->Write(sContent + _T("\"\n"));
1148
 
    m_pPoFile->Write(_T("msgstr \"\"\n\n"));
1149
 
 
1150
 
}
1151
 
 
1152
1204
 
1153
1205
//------------------------------------------------------------------------------------
1154
1206
// Debug methods