File: web.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 (221 lines) | stat: -rw-r--r-- 9,406 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 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/web.h>
#include <export/logic.h>
#include <tasks/logic.h>
#include <database/bibles.h>
#include <database/books.h>
#include <database/logs.h>
#include <database/config/bible.h>
#include <database/state.h>
#include <database/bibleimages.h>
#include <filter/url.h>
#include <filter/string.h>
#include <filter/roles.h>
#include <filter/text.h>
#include <filter/css.h>
#include <filter/usfm.h>
#include <fonts/logic.h>
#include <html/text.h>
#include <html/header.h>
#include <locale/translate.h>
#include <styles/sheets.h>


void export_web_book (std::string bible, int book, bool log)
{
  const std::string directory = export_logic::web_directory (bible);
  if (!file_or_dir_exists (directory)) filter_url_mkdir (directory);
  
  
  const std::string stylesheet = database::config::bible::get_export_stylesheet (bible);
  
  
  const std::string feedback_email = database::config::bible::get_export_feedback_email (bible);
  
  
  // Copy font to the output directory.
  const std::string font = fonts::logic::get_text_font (bible);
  if (!font.empty ()) {
    if (fonts::logic::font_exists (font)) {
      std::string fontpath = fonts::logic::get_font_path (font);
      const std::string contents = filter_url_file_get_contents (fontpath);
      fontpath = filter_url_create_path ({directory, font});
      filter_url_file_put_contents (fontpath, contents);
    }
  }
  
  
  const std::string backLinkPath {export_logic::web_back_link_directory (bible)};
  
  
  const std::string bibleBookText = bible + " " + database::books::get_english_from_id (static_cast<book_id>(book));
  
  
  // Web index file for the book.
  HtmlText html_text_rich_book_index (bibleBookText);
  Html_Header htmlHeader = Html_Header (html_text_rich_book_index);
  htmlHeader.search_back_link (backLinkPath + filter_url_html_file_name_bible ("", book), translate("Go back to") + " " + bibleBookText);
  htmlHeader.create ({
    std::pair (bible, filter_url_html_file_name_bible ()),
    std::pair (translate (database::books::get_english_from_id (static_cast<book_id>(book))), filter_url_html_file_name_bible ())
  });
  html_text_rich_book_index.new_paragraph ("navigationbar");
  html_text_rich_book_index.add_text ("|");
  
  
  // Go through the chapters of this book.
  const std::vector <int> chapters = database::bibles::get_chapters (bible, book);
  for (size_t c = 0; c < chapters.size(); c++) {
    const int chapter = chapters [c];
    const bool is_first_chapter = (c == 0);
    const bool is_last_chapter = (c == chapters.size() - 1);
    
    // The text filter for this chapter.
    Filter_Text filter_text_chapter = Filter_Text (bible);
    
    // Get the USFM for the chapter.
    std::string usfm = database::bibles::get_chapter (bible, book, chapter);
    // Trim it.
    usfm = filter::strings::trim (usfm);
    // Use small chunks of USFM at a time for much better performance.
    filter_text_chapter.add_usfm_code (usfm);
    
    // Interlinked web data for one chapter.
    filter_text_chapter.html_text_linked = new HtmlText (translate("Bible"));
    filter_text_chapter.html_text_linked->custom_class = Filter_Css::getClass (bible);
    
    // Create breadcrumbs and navigator for the chapter.
    Html_Header html_header = Html_Header (*filter_text_chapter.html_text_linked);
    html_header.search_back_link (backLinkPath + filter_url_html_file_name_bible ("", book, chapter), translate("Go back to") + " " + bibleBookText + " " + std::to_string (chapter));
    std::vector <std::pair <std::string, std::string> > breadcrumbs_navigator;
    breadcrumbs_navigator.push_back (std::pair (bible, filter_url_html_file_name_bible ()));
    breadcrumbs_navigator.push_back (std::pair (translate (database::books::get_english_from_id (static_cast<book_id>(book))), filter_url_html_file_name_bible ()));
    if (!is_first_chapter) {
      breadcrumbs_navigator.push_back (std::pair ("«", filter_url_html_file_name_bible ("", book, chapter - 1)));
    }
    breadcrumbs_navigator.push_back (std::pair (std::to_string (chapter), filter_url_html_file_name_bible ("", book)));
    if (!is_last_chapter) {
      breadcrumbs_navigator.push_back (std::pair ("»", filter_url_html_file_name_bible ("", book, chapter + 1)));
    }
    // Optionally add a link for giving feedback by email.
    if (!feedback_email.empty ()) {
      breadcrumbs_navigator.push_back (std::pair ("|", ""));
      std::string subject = translate ("Comment on") + " " + bible + " " + database::books::get_english_from_id (static_cast<book_id>(book)) + " " + std::to_string (chapter);
      subject = filter::strings::replace (" ", "%20", subject);
      std::string link = "mailto:" + feedback_email + "?Subject=" + subject;
      breadcrumbs_navigator.push_back (std::pair (translate ("Feedback"), link));
    }
    html_header.create (breadcrumbs_navigator);
    
    // Create interlinked html for the chapter.
    filter_text_chapter.run (stylesheet);
    filter_text_chapter.html_text_linked->save (filter_url_html_file_name_bible (directory, book, chapter));
    
    html_text_rich_book_index.add_link (html_text_rich_book_index.current_p_node, filter_url_html_file_name_bible ("", book, chapter), "", std::to_string (chapter), "", " " + std::to_string (chapter) + " ");
    html_text_rich_book_index.add_text ("|");
    
    // Save any images that were included.
    for (auto src : filter_text_chapter.image_sources) {
      std::string contents = database::bible_images::get(src);
      std::string filename = filter_url_create_path ({directory, src});
      filter_url_file_put_contents(filename, contents);
    }
  }
  
  
  // Save the book index.
  html_text_rich_book_index.save (filter_url_html_file_name_bible (directory, book));
  
  
  // Clear the flag for this export.
  Database_State::clearExport (bible, book, export_logic::export_web);
  
  
  if (log) {
    Database_Logs::log (translate("Exported to web") + ": " + bible + " " + database::books::get_english_from_id (static_cast<book_id>(book)), roles::translator);
  }
}


void export_web_index (std::string bible, bool log)
{
  // Create folders for the web export.
  std::string directory = export_logic::web_directory (bible);
  if (!file_or_dir_exists (directory)) filter_url_mkdir (directory);
  
  
  // Filenames for the web file and stylesheet.
  std::string indexFile = filter_url_create_path ({directory, "index.html"});
  std::string index00 = filter_url_create_path ({directory, "00_index.html"});
  std::string filecss = filter_url_create_path ({directory, "stylesheet.css"});
  
  
  const std::string stylesheet = database::config::bible::get_export_stylesheet (bible);
  
  
  // Create stylesheet.
  Styles_Sheets styles_sheets;
  styles_sheets.create (stylesheet, filecss, false, bible);
  
  
  std::string backLinkPath = export_logic::web_back_link_directory (bible);
  
  
  // Main index file.
  HtmlText html_text_rich_bible_index (bible);
  // On top are the breadcrumbs, starting with a clickable Bible name.
  Html_Header htmlHeader = Html_Header (html_text_rich_bible_index);
  htmlHeader.search_back_link (backLinkPath + filter_url_html_file_name_bible (), translate("Go back to Bible"));
  htmlHeader.create ({ std::pair (bible, filter_url_html_file_name_bible ())});
  
  
  // Prepare for the list of books in de html index file.
  html_text_rich_bible_index.new_paragraph ("navigationbar");
  html_text_rich_bible_index.add_text (" |");
  
  
  // Go through the Bible books.
  std::vector <int> books = database::bibles::get_books (bible);
  for (auto book : books) {
    // Add this book to the main web index.
    html_text_rich_bible_index.add_link (html_text_rich_bible_index.current_p_node,  filter_url_html_file_name_bible ("", book), "", translate (database::books::get_english_from_id (static_cast<book_id>(book))), "", " " + translate (database::books::get_english_from_id (static_cast<book_id>(book))) + " ");
    html_text_rich_bible_index.add_text ("|");
  }
  
  
  // Save index file for the interlinked web export.
  html_text_rich_bible_index.save (indexFile);
  html_text_rich_bible_index.save (index00);
  
  
  // Lens image supporting search.
  std::string lenspath = filter_url_create_root_path ({"webbb", "lens.png"});
  std::string contents = filter_url_file_get_contents (lenspath);
  lenspath = filter_url_create_path ({directory, "lens.png"});
  filter_url_file_put_contents (lenspath, contents);

  
  // Clear the flag that indicated this export.
  Database_State::clearExport (bible, 0, export_logic::export_web_index);

  
  if (log) Database_Logs::log (translate("Exported to web") + ": " + bible + " Index", roles::translator);
}