File: odt.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (146 lines) | stat: -rw-r--r-- 6,146 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 Copyright (©) 2003-2025 Teus Benschop.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <export/odt.h>
#include <export/logic.h>
#include <tasks/logic.h>
#include <database/bibles.h>
#include <database/books.h>
#include <database/logs.h>
#include <database/state.h>
#include <database/config/bible.h>
#include <database/bibleimages.h>
#include <filter/url.h>
#include <filter/string.h>
#include <filter/roles.h>
#include <filter/text.h>
#include <filter/archive.h>
#include <filter/shell.h>
#include <filter/usfm.h>
#include <filter/passage.h>
#include <locale/translate.h>
#include <styles/sheets.h>


void export_odt_book (std::string bible, int book, bool log)
{
  // Create folders for the OpenDocument export.
  std::string directory = filter_url_create_path ({export_logic::bible_directory (bible), "opendocument"});
  if (!file_or_dir_exists (directory)) filter_url_mkdir (directory);
  
  
  // Filenames for the various types of OpenDocument files.
  std::string basename = export_logic::base_book_filename (bible, book);
  std::string standardFilename = filter_url_create_path ({directory, basename + "_standard.odt"});
  std::string textOnlyFilename = filter_url_create_path ({directory, basename + "_text_only.odt"});
  std::string textAndCitationsFilename = filter_url_create_path ({directory, basename + "_text_and_note_citations.odt"});
  std::string notesFilename = filter_url_create_path ({directory, basename + "_notes.odt"});

  
  const std::string stylesheet = database::config::bible::get_export_stylesheet (bible);
  
  
  Filter_Text filter_text = Filter_Text (bible);
  filter_text.odf_text_standard = new odf_text (bible);
  filter_text.odf_text_text_only = new odf_text (bible);
  filter_text.odf_text_text_and_note_citations = new odf_text (bible);
  filter_text.odf_text_notes = new odf_text (bible);
  
  
  if (book == 0) {
    // Load entire Bible, ordered.
    std::vector <int> books = filter_passage_get_ordered_books (bible);
    for (auto book2 : books) {
      std::vector <int> chapters = database::bibles::get_chapters (bible, book2);
      for (auto chapter : chapters) {
        std::string usfm = database::bibles::get_chapter (bible, book2, chapter);
        usfm = filter::strings::trim (usfm);
        // Use small chunks of USFM at a time for much better performance.
        filter_text.add_usfm_code (usfm);
      }
    }
  } else {
    // Load one book.
    std::vector <int> chapters = database::bibles::get_chapters (bible, book);
    for (auto chapter : chapters) {
      std::string usfm = database::bibles::get_chapter (bible, book, chapter);
      usfm = filter::strings::trim (usfm);
      // Use small chunks of USFM at a time for much better performance.
      filter_text.add_usfm_code (usfm);
    }
  }
  
  
  // Convert the USFM to OpenDocument.
  filter_text.run (stylesheet);
  
  
  // Save text files and optionally the images included in the text.
  filter_text.odf_text_standard->save (standardFilename);
  filter_text.odf_text_text_only->save (textOnlyFilename);
  filter_text.odf_text_text_and_note_citations->save (textAndCitationsFilename);
  filter_text.odf_text_notes->save (notesFilename);
  for (auto src : filter_text.image_sources) {
    std::string contents = database::bible_images::get(src);
    std::string path = filter_url_create_path ({directory, src});
    filter_url_file_put_contents(path, contents);
  }

    
  // Securing the OpenDocument export implies that the exported files are zipped and secured with a password.
  // It uses the external zip binary.
  bool secure = database::config::bible::get_secure_odt_export (bible);
  std::string password = database::config::bible::get_export_password (bible);
  std::string basefile = filter_url_basename (standardFilename);
  filter_url_unlink (standardFilename + ".zip");
  if (secure) {
    filter::shell::run (directory, filter::shell::get_executable(filter::shell::Executable::zip), {"-P", password, basefile + ".zip", basefile}, nullptr, nullptr);
    filter_url_unlink (standardFilename);
  }
  basefile = filter_url_basename (textOnlyFilename);
  filter_url_unlink (textOnlyFilename + ".zip");
  if (secure) {
    filter::shell::run (directory, filter::shell::get_executable(filter::shell::Executable::zip), {"-P", password, basefile + ".zip", basefile}, nullptr, nullptr);
    filter_url_unlink (textOnlyFilename);
  }
  basefile = filter_url_basename (textAndCitationsFilename);
  filter_url_unlink (textAndCitationsFilename + ".zip");
  if (secure) {
    filter::shell::run (directory, filter::shell::get_executable(filter::shell::Executable::zip), {"-P", password, basefile + ".zip", basefile}, nullptr, nullptr);
    filter_url_unlink (textAndCitationsFilename);
  }
  basefile = filter_url_basename (notesFilename);
  filter_url_unlink (notesFilename + ".zip");
  if (secure) {
    filter::shell::run (directory, filter::shell::get_executable(filter::shell::Executable::zip), {"-P", password, basefile + ".zip", basefile}, nullptr, nullptr);
    filter_url_unlink (notesFilename);
  }
  
  
  // Clear the flag that indicated this export.
  Database_State::clearExport (bible, book, export_logic::export_opendocument);

  
  if (log) {
    std::string bookname;
    if (book) bookname = database::books::get_english_from_id (static_cast<book_id>(book));
    else bookname = translate ("whole Bible");
    Database_Logs::log (translate("Exported to OpenDocument files") + " " + bible + " " + bookname, roles::translator);
  }
}