get_title.c 2.8 KB

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