comparison mupdf-source/thirdparty/gumbo-parser/examples/clean_text.cc @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright 2013 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Author: jdtang@google.com (Jonathan Tang)
16 //
17 // Gets the cleantext of a page.
18
19 #include <fstream>
20 #include <iostream>
21 #include <stdlib.h>
22 #include <string>
23
24 #include "gumbo.h"
25
26 static std::string cleantext(GumboNode* node) {
27 if (node->type == GUMBO_NODE_TEXT) {
28 return std::string(node->v.text.text);
29 } else if (node->type == GUMBO_NODE_ELEMENT &&
30 node->v.element.tag != GUMBO_TAG_SCRIPT &&
31 node->v.element.tag != GUMBO_TAG_STYLE) {
32 std::string contents = "";
33 GumboVector* children = &node->v.element.children;
34 for (unsigned int i = 0; i < children->length; ++i) {
35 const std::string text = cleantext((GumboNode*) children->data[i]);
36 if (i != 0 && !text.empty()) {
37 contents.append(" ");
38 }
39 contents.append(text);
40 }
41 return contents;
42 } else {
43 return "";
44 }
45 }
46
47 int main(int argc, char** argv) {
48 if (argc != 2) {
49 std::cout << "Usage: clean_text <html filename>\n";
50 exit(EXIT_FAILURE);
51 }
52 const char* filename = argv[1];
53
54 std::ifstream in(filename, std::ios::in | std::ios::binary);
55 if (!in) {
56 std::cout << "File " << filename << " not found!\n";
57 exit(EXIT_FAILURE);
58 }
59
60 std::string contents;
61 in.seekg(0, std::ios::end);
62 contents.resize(in.tellg());
63 in.seekg(0, std::ios::beg);
64 in.read(&contents[0], contents.size());
65 in.close();
66
67 GumboOutput* output = gumbo_parse(contents.c_str());
68 std::cout << cleantext(output->root) << std::endl;
69 gumbo_destroy_output(&kGumboDefaultOptions, output);
70 }