positions_of_class.cc 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // Print out the positions of all elements with a certain CSS class.
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <algorithm>
  21. #include <fstream>
  22. #include <iostream>
  23. #include <string>
  24. #include "gumbo.h"
  25. static std::string find_line(
  26. const std::string& original_text, const GumboAttribute& attr) {
  27. size_t attr_index = attr.original_value.data - original_text.data();
  28. size_t begin = original_text.rfind("\n", attr_index) + 1;
  29. size_t end = original_text.find("\n", attr_index);
  30. if (end != std::string::npos) {
  31. end--;
  32. } else {
  33. end = (size_t) original_text.length() - 1;
  34. }
  35. end = std::min(end, attr_index + 40);
  36. begin = std::max(begin, attr_index - 40);
  37. return original_text.substr(begin, end - begin);
  38. }
  39. static void search_for_class(
  40. GumboNode* node, const std::string& original_text, const char* cls_name) {
  41. if (node->type != GUMBO_NODE_ELEMENT) {
  42. return;
  43. }
  44. GumboAttribute* cls_attr;
  45. if ((cls_attr = gumbo_get_attribute(&node->v.element.attributes, "class")) &&
  46. strstr(cls_attr->value, cls_name) != NULL) {
  47. std::cout << cls_attr->value_start.line << ":"
  48. << cls_attr->value_start.column << " - "
  49. << find_line(original_text, *cls_attr) << std::endl;
  50. }
  51. GumboVector* children = &node->v.element.children;
  52. for (int i = 0; i < children->length; ++i) {
  53. search_for_class(
  54. static_cast<GumboNode*>(children->data[i]), original_text, cls_name);
  55. }
  56. }
  57. int main(int argc, char** argv) {
  58. if (argc != 3) {
  59. std::cout << "Usage: positions_of_class <html filename> <CSS classname>.\n";
  60. exit(EXIT_FAILURE);
  61. }
  62. const char* filename = argv[1];
  63. const char* cls = argv[2];
  64. std::ifstream in(filename, std::ios::in | std::ios::binary);
  65. if (!in) {
  66. std::cout << "File " << filename << " not found!\n";
  67. exit(EXIT_FAILURE);
  68. }
  69. std::string contents;
  70. in.seekg(0, std::ios::end);
  71. contents.resize(in.tellg());
  72. in.seekg(0, std::ios::beg);
  73. in.read(&contents[0], contents.size());
  74. in.close();
  75. // If you used contents.c_str(), it'd be harder to match up original
  76. // positions, because c_str() creates a copy of the string and you can't do
  77. // pointer arithmetic betweent contents.data() and the original_* pointers.
  78. GumboOutput* output = gumbo_parse_with_options(
  79. &kGumboDefaultOptions, contents.data(), contents.length());
  80. search_for_class(output->root, contents, cls);
  81. gumbo_destroy_output(&kGumboDefaultOptions, output);
  82. }