Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/gumbo-parser/examples/get_title.c @ 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 // Retrieves the title of a page. | |
| 18 | |
| 19 #include <assert.h> | |
| 20 #include <stdio.h> | |
| 21 #include <stdlib.h> | |
| 22 #include <sys/stat.h> | |
| 23 | |
| 24 #include "gumbo.h" | |
| 25 | |
| 26 static void read_file(FILE* fp, char** output, int* length) { | |
| 27 struct stat filestats; | |
| 28 int fd = fileno(fp); | |
| 29 fstat(fd, &filestats); | |
| 30 *length = filestats.st_size; | |
| 31 *output = malloc(*length + 1); | |
| 32 int start = 0; | |
| 33 int bytes_read; | |
| 34 while ((bytes_read = fread(*output + start, 1, *length - start, fp))) { | |
| 35 start += bytes_read; | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 static const char* find_title(const GumboNode* root) { | |
| 40 assert(root->type == GUMBO_NODE_ELEMENT); | |
| 41 assert(root->v.element.children.length >= 2); | |
| 42 | |
| 43 const GumboVector* root_children = &root->v.element.children; | |
| 44 GumboNode* head = NULL; | |
| 45 for (int i = 0; i < root_children->length; ++i) { | |
| 46 GumboNode* child = root_children->data[i]; | |
| 47 if (child->type == GUMBO_NODE_ELEMENT && | |
| 48 child->v.element.tag == GUMBO_TAG_HEAD) { | |
| 49 head = child; | |
| 50 break; | |
| 51 } | |
| 52 } | |
| 53 assert(head != NULL); | |
| 54 | |
| 55 GumboVector* head_children = &head->v.element.children; | |
| 56 for (int i = 0; i < head_children->length; ++i) { | |
| 57 GumboNode* child = head_children->data[i]; | |
| 58 if (child->type == GUMBO_NODE_ELEMENT && | |
| 59 child->v.element.tag == GUMBO_TAG_TITLE) { | |
| 60 if (child->v.element.children.length != 1) { | |
| 61 return "<empty title>"; | |
| 62 } | |
| 63 GumboNode* title_text = child->v.element.children.data[0]; | |
| 64 assert(title_text->type == GUMBO_NODE_TEXT || title_text->type == GUMBO_NODE_WHITESPACE); | |
| 65 return title_text->v.text.text; | |
| 66 } | |
| 67 } | |
| 68 return "<no title found>"; | |
| 69 } | |
| 70 | |
| 71 int main(int argc, const char** argv) { | |
| 72 if (argc != 2) { | |
| 73 printf("Usage: get_title <html filename>.\n"); | |
| 74 exit(EXIT_FAILURE); | |
| 75 } | |
| 76 const char* filename = argv[1]; | |
| 77 | |
| 78 FILE* fp = fopen(filename, "r"); | |
| 79 if (!fp) { | |
| 80 printf("File %s not found!\n", filename); | |
| 81 exit(EXIT_FAILURE); | |
| 82 } | |
| 83 | |
| 84 char* input; | |
| 85 int input_length; | |
| 86 read_file(fp, &input, &input_length); | |
| 87 GumboOutput* output = gumbo_parse_with_options( | |
| 88 &kGumboDefaultOptions, input, input_length); | |
| 89 const char* title = find_title(output->root); | |
| 90 printf("%s\n", title); | |
| 91 gumbo_destroy_output(&kGumboDefaultOptions, output); | |
| 92 free(input); | |
| 93 } |
