GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — readable/readable.c at master · fiam/readable Expand file tree/ Copy pathreadable.cMore file actions 1561 lines (1455 loc) · 49.5 KB / Copy pathreadable.cFile metadata and controls1561 lines (1455 loc) · 49.5 KB 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 #define _BSD_SOURCE #include <string.h> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <stdbool.h> #include <stdint.h> #undef _BSD_SOURCE #include <libxml/HTMLparser.h> #include <libxml/HTMLtree.h> #include <libxml/xpath.h> #ifdef READABLE_USE_LIBICU #include <unicode/uregex.h> #include <unicode/ustring.h> #else #include <pcre.h> #endif #include "rd_list.h" #include "khash.h" #include "readable.h" #define UNLIKELY_CANDIDATES_RE "combx|comment|community|disqus|extra|foot|header|menu|remark|rss|shoutbox|sidebar|sponsor|ad-break|agegate|pagination|pager|popup|jobs|selector" #define OK_MAYBE_ITS_A_CANDIDATE_RE "and|article|body|column|main|shadow" #define POSITIVE_SCORE_RE "article|body|content|entry|hentry|main|page|pagination|post|text|blog|story" #define NEGATIVE_SCORE_RE "combx|comment|com-|contact|foot|footer|footnote|masthead|media|meta|outbrain|promo|related|scroll|shoutbox|sidebar|sponsor|shopping|tags|tool|widget" #define SMALL_TEXT_RE "\\.( |$)" #define VIDEO_RE "http:\\/\\/(www\\.)?(youtube|vimeo)\\.com" #define UNLIKELY_ARTICLE_IMAGE_RE "[/_\\-]icon[/_\\-]" #ifdef MAX #undef MAX #endif #define MAX(a,b) ({ __typeof__ (a) _a = (a); \ __typeof__ (b) _b = (b); \ _a > _b ? _a : _b; }) #ifdef MIN #undef MIN #endif #define MIN(a,b) ({ __typeof__ (a) _a = (a); \ __typeof__ (b) _b = (b); \ _a < _b ? _a : _b; }) KHASH_MAP_INIT_STR(str, int); #define __pointer_hash(x) (uintptr_t)(x) #define __pointer_equal(x, y) (x == y) KHASH_INIT(score, htmlNodePtr, float, 1, __pointer_hash, __pointer_equal); #ifdef READABLE_USE_LIBICU UChar *uastrdup(const char *s) { int len = strlen(s); UChar *us = malloc(sizeof(UChar) * (len + 1)); u_uastrcpy(us, s); return us; } #define RD_RE URegularExpression #define matches(re, s) ({ \ UErrorCode status = U_ZERO_ERROR; \ UChar *cs = uastrdup((const char *)s); \ uregex_setText(re, cs, -1, &status); \ UBool result = uregex_find(re, 0, &status); \ uregex_reset(re, 0, &status); \ free(cs); \ result; \ }) #define INITIALIZE_RE(re, s) do { \ UParseError pe; \ UErrorCode status = U_ZERO_ERROR; \ re = uregex_openC(s, UREGEX_CASE_INSENSITIVE, &pe, &status); \ if (U_FAILURE(status)) { \ fprintf(stderr, "Error at offset %d compiling %s: %s", \ pe.offset, s, u_errorName(status)); \ exit(1); \ } \ } while (0) #define FINALIZE_RE(re) uregex_close(re) #else #define RD_RE pcre #define matches(re, s) (pcre_exec(re, NULL, (char *)s, strlen((char *)s), 0, 0, NULL, 0) >= 0) #define INITIALIZE_RE(pointer, re) do { \ const char *error = NULL; \ int erroroffset; \ pointer = pcre_compile(re, PCRE_CASELESS | PCRE_UTF8, &error, &erroroffset, NULL); \ if (pointer == NULL) { \ fprintf(stderr, "Error at offset %d compiling %s: %s", \ erroroffset, re, error); \ exit(1); \ } \ } while(0) #define FINALIZE_RE(re) pcre_free(re) #endif static RD_RE *UNLIKELY_CANDIDATES = NULL; static RD_RE *OK_MAYBE_ITS_A_CANDIDATE = NULL; static RD_RE *POSITIVE_SCORE = NULL; static RD_RE *NEGATIVE_SCORE = NULL; static RD_RE *SMALL_TEXT = NULL; static RD_RE *VIDEO = NULL; static RD_RE *UNLIKELY_ARTICLE_IMAGE = NULL; #define HTML_READ_DOC_OPTIONS (HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING) #ifdef DEBUG_LOG #undef DEBUG_LOG #endif #ifdef READABLE_DEBUG #define DEBUG_LOG(...) fprintf(stderr, __VA_ARGS__) char * node_debug_name(htmlNodePtr node) { char buf[2048]; memset(buf, '\0', sizeof(buf)); strcat(buf, (char *)node->name); xmlChar *node_id = xmlGetProp(node, BAD_CAST "id"); if (node_id) { strcat(buf, "#"); strcat(buf, (char *)node_id); xmlFree(node_id); } xmlChar *node_class = xmlGetProp(node, BAD_CAST "class"); if (node_class) { strcat(buf, "."); strcat(buf, (char *)node_class); xmlFree(node_class); } sprintf(buf + strlen(buf), "@%p", node); return strdup(buf); } #else #define DEBUG_LOG(...) #endif #define lookup_count(tbl, key, def) ({ khiter_t ii = kh_get_str(tbl, key); (ii == kh_end(tbl)) ? 0 : kh_value(tbl, ii); }) #define lookup_score(tbl, key) ({ khiter_t ii = kh_get_score(tbl, key); (ii == kh_end(tbl)) ? 0 : kh_value(tbl, ii); }) #define lookup_score_ptr(tbl, key) ({ khiter_t ii = kh_get_score(tbl, key); (ii == kh_end(tbl)) ? NULL : &(kh_value(tbl, ii)); }) __attribute__ ((constructor)) void initialize_regexps(void) { INITIALIZE_RE(UNLIKELY_CANDIDATES, UNLIKELY_CANDIDATES_RE); INITIALIZE_RE(OK_MAYBE_ITS_A_CANDIDATE, OK_MAYBE_ITS_A_CANDIDATE_RE); INITIALIZE_RE(POSITIVE_SCORE, POSITIVE_SCORE_RE); INITIALIZE_RE(NEGATIVE_SCORE, NEGATIVE_SCORE_RE); INITIALIZE_RE(SMALL_TEXT, SMALL_TEXT_RE); INITIALIZE_RE(VIDEO, VIDEO_RE); INITIALIZE_RE(UNLIKELY_ARTICLE_IMAGE, UNLIKELY_ARTICLE_IMAGE_RE); } __attribute__ ((destructor)) void finalize_regexps(void) { FINALIZE_RE(UNLIKELY_CANDIDATES); FINALIZE_RE(OK_MAYBE_ITS_A_CANDIDATE); FINALIZE_RE(POSITIVE_SCORE); FINALIZE_RE(NEGATIVE_SCORE); FINALIZE_RE(SMALL_TEXT); FINALIZE_RE(VIDEO); FINALIZE_RE(UNLIKELY_ARTICLE_IMAGE); #ifdef READABLE_USE_LIBICU #if !TARGET_OS_IPHONE /* Including this code in iOS triggers the non-public API detection and prevents app submission */ extern void u_cleanup(void); u_cleanup(); #endif #endif } void readable_error_handler(void *userdata, xmlErrorPtr error) { if (error->level == XML_ERR_FATAL) { fprintf(stderr, "Error on %s:%d->%s", error->file, error->line, error->message); } } __attribute__ ((constructor)) void initialize_error_handler(void) { xmlSetStructuredErrorFunc(NULL, readable_error_handler); } void xfree(void *p) { if (p) { free(p); } } char * node_test_name(htmlNodePtr node) { xmlChar *node_class = xmlGetProp(node, BAD_CAST "class"); xmlChar *node_id = xmlGetProp(node, BAD_CAST "id"); int class_len; int len; char *val = NULL; if (node_class && node_id) { class_len = xmlStrlen(node_class); len = class_len + xmlStrlen(node_id) + 1; val = malloc(len + 1); strncpy(val, (char *)node_class, len); strncpy(val + class_len, (char *)node_id, len - class_len); val[len] = '\0'; } else if (node_class) { val = strdup((const char *)node_class); } else if (node_id) { val = strdup((const char *)node_id); } xfree(node_class); xfree(node_id); return val; } bool should_alter_to_p(htmlNodePtr node) { for(htmlNodePtr cur = node->children; cur; cur = cur->next) { if (xmlStrEqual(cur->name, BAD_CAST "a") || xmlStrEqual(cur->name, BAD_CAST "blockquote") || xmlStrEqual(cur->name, BAD_CAST "dl") || xmlStrEqual(cur->name, BAD_CAST "div") || xmlStrEqual(cur->name, BAD_CAST "ol") || xmlStrEqual(cur->name, BAD_CAST "p") || xmlStrEqual(cur->name, BAD_CAST "pre") || xmlStrEqual(cur->name, BAD_CAST "table") || xmlStrEqual(cur->name, BAD_CAST "ul")) { return false; } bool val = should_alter_to_p(cur); if (!val) { return false; } } return true; } void node_inner_text_recursive(htmlNodePtr node, char **data, ssize_t *data_size, ssize_t *data_allocated_size) { if (node->type == XML_TEXT_NODE && node->content) { ssize_t available_size = *data_allocated_size - *data_size - 1; ssize_t len = xmlStrlen(node->content); if (len > available_size) { while (len > available_size) { *data_allocated_size = MAX(*data_allocated_size * 1.2, 512); available_size = *data_allocated_size - *data_size - 1; } *data = realloc(*data, *data_allocated_size); } strncpy(*data + *data_size, (const char*)node->content, len); *data_size += len; (*data)[*data_size] = '\0'; } for (htmlNodePtr cur = node->children; cur; cur = cur->next) { node_inner_text_recursive(cur, data, data_size, data_allocated_size); } } char * node_inner_text(htmlNodePtr node) { ssize_t data_size = 0; ssize_t data_allocated_size = 0; char *data = NULL; node_inner_text_recursive(node, &data, &data_size, &data_allocated_size); return data; } char * node_html(htmlDocPtr doc, htmlNodePtr node) { char *retval; xmlBufferPtr buffer = xmlBufferCreateSize(1024); xmlOutputBufferPtr outputBuffer = xmlOutputBufferCreateBuffer(buffer, NULL); htmlNodeDumpFormatOutput(outputBuffer, doc, node, "UTF-8", 0); xmlOutputBufferFlush(outputBuffer); const xmlChar *bufferContents = xmlBufferContent(buffer); retval = strdup((char *)bufferContents); xmlOutputBufferClose(outputBuffer); xmlBufferFree(buffer); return retval; } char * node_inner_html(htmlDocPtr doc, htmlNodePtr node) { ssize_t allocated_size = 0; ssize_t data_size = 0; char *html = NULL; for (htmlNodePtr cur = node->children; cur; cur = cur->next) { char *cur_html = node_html(doc, cur); int len = strlen(cur_html); ssize_t available_size = allocated_size - data_size - 1; if (len > available_size) { while (len > available_size) { allocated_size = MAX(allocated_size * 1.2, 512); available_size = allocated_size - data_size - 1; } html = realloc(html, allocated_size); } strncpy(html + data_size, cur_html, len); data_size += len; html[data_size] = '\0'; free(cur_html); } return html; } int number_of_commas(const char *text) { int c = 0; for (const char *p = text; *p; p++) { if (*p == ',') { c++; } } return c; } int node_text_len(htmlNodePtr node) { int len = 0; char *inner_text = node_inner_text(node); if (inner_text) { len = strlen(inner_text); free(inner_text); } return len; } int node_nospaces_len(htmlNodePtr node) { int nospaces = 0; char *node_text = node_inner_text(node); if (node_text) { for (char *p = node_text; *p; ++p) { if (!isspace(*p)) { nospaces++; } } free(node_text); } return nospaces; } int anchors_text_len(htmlNodePtr node) { int len = 0; if (xmlStrEqual(node->name, BAD_CAST "a")) { len += node_text_len(node); } for (htmlNodePtr cur = node->children; cur; cur = cur->next) { len += anchors_text_len(cur); } return len; } float node_link_density(htmlNodePtr node) { int text_len = node_text_len(node); int anchors_len = anchors_text_len(node); if (text_len == 0) { return anchors_len; } return ((float)anchors_len) / text_len; } float node_score(htmlNodePtr node) { const xmlChar *name = node->name; if (xmlStrEqual(name, BAD_CAST "div")) { return 5; } if (xmlStrEqual(name, BAD_CAST "pre") || xmlStrEqual(name, BAD_CAST "td") || xmlStrEqual(name, BAD_CAST "blockquote")) { return 3; } if (xmlStrEqual(name, BAD_CAST "address") || xmlStrEqual(name, BAD_CAST "ol") || xmlStrEqual(name, BAD_CAST "ul") || xmlStrEqual(name, BAD_CAST "dl") || xmlStrEqual(name, BAD_CAST "dd") || xmlStrEqual(name, BAD_CAST "dt") || xmlStrEqual(name, BAD_CAST "li") || xmlStrEqual(name, BAD_CAST "form")) { return -3; } if (xmlStrEqual(name, BAD_CAST "h1") || xmlStrEqual(name, BAD_CAST "h2") || xmlStrEqual(name, BAD_CAST "h3") || xmlStrEqual(name, BAD_CAST "h4") || xmlStrEqual(name, BAD_CAST "h5") || xmlStrEqual(name, BAD_CAST "h6") || xmlStrEqual(name, BAD_CAST "th")) { return -5; } return 0; } float name_score(const xmlChar *name) { float score = 0; if (matches(POSITIVE_SCORE, name)) { score += 25; } if (matches(NEGATIVE_SCORE, name)) { score -= 25; } return score; } float node_class_score(htmlNodePtr node) { float score = 0; xmlChar *node_class = xmlGetProp(node, BAD_CAST "class"); if (node_class) { score += name_score(node_class); free(node_class); } xmlChar *node_id = xmlGetProp(node, BAD_CAST "id"); if (node_id) { score += name_score(node_id); free(node_id); } return score; } float * initialize_node_score(kh_score_t *scores, htmlNodePtr node, int options) { float score = node_score(node); if (options & READABLE_OPTION_WEIGHT_CLASSES) { score += node_class_score(node); } int ret; khiter_t iter = kh_put_score(scores, node, &ret); kh_value(scores, iter) = score; return &(kh_value(scores, iter)); } void append_readable_node(htmlNodePtr readable, htmlNodePtr node) { if (!xmlStrEqual(node->name, BAD_CAST "div") && !xmlStrEqual(node->name, BAD_CAST "p")) { xmlFree((void *)node->name); node->name = xmlCharStrdup("div"); } xmlUnlinkNode(node); xmlAddChild(readable, node); } bool iframe_looks_like_video(htmlNodePtr node) { bool retval = false; xmlChar *src = xmlGetProp(node, BAD_CAST "src"); if (src ) { if (strstr((char *)src, "youtube") || strstr((char *)src, "vimeo")) { retval = true; } free(src); } return retval; } bool object_looks_like_video(htmlNodePtr node) { for (htmlNodePtr cur = node->children; cur; cur = cur->next) { if (xmlStrEqual(cur->name, BAD_CAST "param")) { for (xmlAttrPtr attr = cur->properties; attr; attr = attr->next) { if (xmlStrEqual(attr->name, BAD_CAST "value") && attr->children) { xmlChar *value = attr->children->content; if (strstr((char *)value, "youtube") || strstr((char *)value, "vimeo")) { return true; } } } } } return false; } bool node_looks_like_video(htmlNodePtr node) { if (xmlStrEqual(node->name, BAD_CAST "iframe") || xmlStrEqual(node->name, BAD_CAST "embed")) { return iframe_looks_like_video(node); } else if (xmlStrEqual(node->name, BAD_CAST "object")) { return object_looks_like_video(node); } return false; } void node_tags_count_recursive(htmlNodePtr node, kh_str_t *tags) { int ret; khiter_t iter = kh_get_str(tags, (char *)node->name); int count = 1; if (iter == kh_end(tags)) { iter = kh_put_str(tags, (char *)node->name, &ret); } else { count = kh_value(tags, iter) + 1; } kh_value(tags, iter) = count; for (htmlNodePtr cur = node->children; cur; cur = cur->next) { node_tags_count_recursive(cur, tags); } } kh_str_t * node_tags_count(htmlNodePtr node) { kh_str_t *tags = kh_init_str(); node_tags_count_recursive(node, tags); return tags; } void node_video_count(htmlNodePtr node, int *count) { if (node_looks_like_video(node)) { ++(*count); } for (htmlNodePtr cur = node->children; cur; cur = cur->next) { node_video_count(cur, count); } } bool clean_node_conditionally(htmlNodePtr node, kh_score_t *scores, int *node_images, int options) { float node_score = lookup_score(scores, node); float class_score = (options & READABLE_OPTION_WEIGHT_CLASSES) ? node_class_score(node) : 0; if (node_score + class_score < 0) { #ifdef READABLE_DEBUG char *debug_name = node_debug_name(node); DEBUG_LOG("Removing node %s (score < 0)\n", debug_name); free(debug_name); #endif return true; } bool clean = false; int commas = 0; int text_len = 0; char *node_text = node_inner_text(node); if (node_text) { commas = number_of_commas(node_text); text_len = strlen(node_text); free(node_text); } if (commas < 10) { kh_str_t *tags = node_tags_count(node); int p_count = lookup_count(tags, "p", 0); int img_count = lookup_count(tags, "img", 0); int li_count = lookup_count(tags, "li", 0) - 100; int input_count = lookup_count(tags, "input", 0); float link_density = node_link_density(node); if (((li_count > p_count && node->name[0] != '\0' && node->name[0] != 'o' && node->name[0] != 'u' && node->name[1] != 'l')) || (input_count > floor(p_count / 3.0)) || (text_len < 25 && (img_count == 0 || img_count > 2)) || (class_score < 25 && link_density > 0.2) || (class_score >= 25 && link_density > 0.5)) { clean = true; #ifdef READABLE_DEBUG char *debug_name = node_debug_name(node); DEBUG_LOG("Removing node %s (first condition)\n", debug_name); DEBUG_LOG("p:%d\timg:%d\tli:%d\tinput:%d\tld:%f\tcs:%f\n", p_count, img_count, li_count, input_count, link_density, class_score); free(debug_name); #endif } else if (img_count > p_count && link_density > 0 && text_len > 0) { clean = true; #ifdef READABLE_DEBUG char *debug_name = node_debug_name(node); DEBUG_LOG("Removing node %s (second condition)\n", debug_name); DEBUG_LOG("p:%d\timg:%d\tli:%d\tinput:%d\tld:%f\tcs:%f\n", p_count, img_count, li_count, input_count, link_density, class_score); free(debug_name); #endif } else { int might_be_video_count = lookup_count(tags, "object", 0) + lookup_count(tags, "embed", 0) + lookup_count(tags, "iframe", 0); int video_count = 0; if (might_be_video_count) { node_video_count(node, &video_count); } if ((video_count == 1 && text_len < 75) || video_count > 1 || (xmlStrEqual(node->name, BAD_CAST "table") && video_count != 1 && img_count != 1)) { clean = true; #ifdef READABLE_DEBUG char *debug_name = node_debug_name(node); DEBUG_LOG("Removing node %s (third condition)\n", debug_name); free(debug_name); #endif } } *node_images = img_count; kh_destroy_str(tags); } return clean; } htmlNodePtr clean_node(htmlDocPtr doc, htmlNodePtr node, kh_score_t *scores, int options, int *nimages) { #ifdef READABLE_DEBUG char *debug_name = node_debug_name(node); DEBUG_LOG("Cleaning node %s\n", debug_name); free(debug_name); #endif if (node->name && node->name[0] == 'h') { if (xmlStrEqual(node->name, BAD_CAST "h1")) { xmlUnlinkNode(node); xmlFreeNode(node); return NULL; } if (xmlStrEqual(node->name, BAD_CAST "h2") || xmlStrEqual(node->name, BAD_CAST "h3") || xmlStrEqual(node->name, BAD_CAST "h4")) { if (node_class_score(node) < 0 || node_link_density(node) > 0.33) { xmlUnlinkNode(node); xmlFreeNode(node); return NULL; } } } /* TODO: If there's only one h2, remove it */ if (xmlStrEqual(node->name, BAD_CAST "font")) { /* Make it a span */ xmlFree((void *)node->name); node->name = xmlCharStrdup("span"); } if (xmlStrEqual(node->name, BAD_CAST "iframe")) { if (!iframe_looks_like_video(node)) { xmlUnlinkNode(node); xmlFreeNode(node); return NULL; } } if (xmlStrEqual(node->name, BAD_CAST "img")) { if (options & READABLE_OPTION_REMOVE_IMAGES) { xmlUnlinkNode(node); xmlFreeNode(node); return NULL; } xmlChar *src = xmlGetProp(node, BAD_CAST "src"); if (!src || !src[0]) { xfree(src); xmlUnlinkNode(node); xmlFreeNode(node); return NULL; } xmlChar *alt = xmlGetProp(node, BAD_CAST "alt"); xmlChar *title = xmlGetProp(node, BAD_CAST "title"); #define xlen(x) (x ? strlen((char *)x) : 0) int len = xlen(alt) + xlen(title) + strlen((char *)src); char *test = malloc(len + 1); strcpy(test, (char *)src); free(src); if (alt) { strcat(test, (char *)alt); free(alt); } if (title) { strcat(test, (char *)title); free(title); } DEBUG_LOG("Testing image %s\n", test); int match = matches(UNLIKELY_ARTICLE_IMAGE, test); free(test); if (match) { #ifdef READABLE_DEBUG char *debug_name = node_debug_name(node); DEBUG_LOG("Removing unlikely image %s\n", debug_name); free(debug_name); #endif xmlUnlinkNode(node); xmlFreeNode(node); return NULL; } ++(*nimages); } if ((xmlStrEqual(node->name, BAD_CAST "object") || xmlStrEqual(node->name, BAD_CAST "embed")) && !node_looks_like_video(node)) { xmlUnlinkNode(node); xmlFreeNode(node); return NULL; } if (!htmlTagLookup(node->name)) { /* When parsing bad html, a p might end in an unknown tag. It won't happen with a div, since the parser will always start a new tag for it as a sibling of the unknown tag. */ htmlNodePtr cur = node->children; while (cur) { /* See comment a few lines down, with the same bucle structure */ htmlNodePtr next = cur->next; if (cur->name[0] == 'p') { xmlAddNextSibling(node, cur); clean_node(doc, cur, scores, options, nimages); } cur = next; } xmlUnlinkNode(node); xmlFreeNode(node); return NULL; } htmlNodePtr cur = node->children; while(cur) { /* Grab the next node first, since clean_node() might free the memory associated with the current node */ htmlNodePtr next = cur->next; if (cur->type == XML_ELEMENT_NODE) { clean_node(doc, cur, scores, options, nimages); } cur = next; } /* Do these clean operations after removing any children, since they depend on the node content which might be altered while cleaning it */ if (options & READABLE_OPTION_CLEAN_CONDITIONALLY) { if (xmlStrEqual(node->name, BAD_CAST "form") || xmlStrEqual(node->name, BAD_CAST "table") || xmlStrEqual(node->name, BAD_CAST "ul") || xmlStrEqual(node->name, BAD_CAST "div")) { int node_images = 0; if (clean_node_conditionally(node, scores, &node_images, options)) { *nimages -= node_images; xmlUnlinkNode(node); xmlFreeNode(node); return NULL; } } } if (node->name[0] == 'p') { int nospaces = node_nospaces_len(node); if (!nospaces) { kh_str_t *tags = node_tags_count(node); int img_count = lookup_count(tags, "img", 0); int embed_count = lookup_count(tags, "embed", 0); int object_count = lookup_count(tags, "object", 0); int iframe_count = lookup_count(tags, "iframe", 0); kh_destroy_str(tags); if (!img_count && !embed_count && !object_count && !iframe_count) { xmlUnlinkNode(node); xmlFreeNode(node); return NULL; } } } for (xmlAttrPtr attr = node->properties, next; attr; attr = next) { next = attr->next; if (xmlStrEqual(attr->name, BAD_CAST "style") || xmlStrEqual(attr->name, BAD_CAST "border") || xmlStrEqual(attr->name, BAD_CAST "class") || xmlStrEqual(attr->name, BAD_CAST "target") || (attr->name[0] == 'o' && attr->name[1] == 'n')) { xmlRemoveProp(attr); } else if (xmlStrEqual(attr->name, BAD_CAST "href")) { if (xmlStrstr(attr->children->content, BAD_CAST "javascript") == attr->children->content) { xmlRemoveProp(attr); } } } return node; } struct rd_list * add_images_to_list(htmlNodePtr node, struct rd_list *list) { if (xmlStrEqual(node->name, BAD_CAST "img")) { list = rd_list_append(list, node); } for (htmlNodePtr cur = node->children; cur; cur = cur->next) { if (cur->type == XML_ELEMENT_NODE) { list = add_images_to_list(cur, list); } } return list; } htmlNodePtr search_article_image(htmlNodePtr node, htmlNodePtr prev) { struct rd_list *candidates = NULL; kh_score_t *scores = kh_init_score(); float initial_score = -3; int len = 0; int ret; while (node) { for (htmlNodePtr cur = node->children; cur; cur = cur->next) { if (cur != prev && cur->type == XML_ELEMENT_NODE) { candidates = add_images_to_list(cur, candidates); for (; len < rd_list_length(candidates); ++len) { htmlNodePtr image = rd_list_index_data(candidates, len); khiter_t iter = kh_put_score(scores, image, &ret); kh_value(scores, iter) = initial_score; } } } /* Reduce the score as we climb the tree */ initial_score *= 5; prev = node; node = node->parent; } htmlNodePtr top_image = NULL; float top_score = 0; for (int ii = 0; ii < rd_list_length(candidates); ++ii) { htmlNodePtr image = rd_list_index_data(candidates, ii); float score = lookup_score(scores, image); xmlChar *src = xmlGetProp(image, BAD_CAST "src"); if (!src) { continue; } #ifdef READABLE_DEBUG char *debug_name = node_debug_name(image); DEBUG_LOG("Image candidate %s (%s)\n", debug_name, src); free(debug_name); #endif xmlChar *width = xmlGetProp(image, BAD_CAST "width"); xmlChar *height = xmlGetProp(image, BAD_CAST "height"); if (matches(UNLIKELY_ARTICLE_IMAGE, src)) { score -= 20; } char *dot = strrchr((char *)src, '.'); if (dot) { for (char *p = dot; *p; ++p) { *p = tolower(*p); } dot++; if (strcmp(dot, "jpg") == 0 || strcmp(dot, "jpeg") == 0) { score += 15; } else if (strcmp(dot, "gif") == 0) { score -= 10; } } if (width && height && MIN(atoi((char *)width), atoi((char *)height)) > 150) { score += 10; } if (score > top_score) { top_image = image; top_score = score; } xfree(src); xfree(width); xfree(height); } if (top_score < 0) { top_image = NULL; } if (top_image) { xmlAttrPtr attr = top_image->properties; while (attr) { xmlAttrPtr next = attr->next; if (!xmlStrEqual(attr->name, BAD_CAST "width") && !xmlStrEqual(attr->name, BAD_CAST "height") && !xmlStrEqual(attr->name, BAD_CAST "src") && !xmlStrEqual(attr->name, BAD_CAST "alt")) { xmlRemoveProp(attr); } attr = next; } } rd_list_free(candidates); kh_destroy_score(scores); return top_image; } htmlNodePtr score_nodes(htmlNodePtr node, struct rd_list **list, int options) { /* If list == NULL, we're working with the root node, so we shouldn't remove it */ if ((options & READABLE_OPTION_STRIP_UNLIKELYS) && *list) { char *test_name = node_test_name(node); if (test_name) { if (matches(UNLIKELY_CANDIDATES, test_name) && !matches(OK_MAYBE_ITS_A_CANDIDATE, test_name)) { #ifdef READABLE_DEBUG char *debug_name = node_debug_name(node); DEBUG_LOG("Removing unlikely candidate %s\n", debug_name); free(debug_name); #endif htmlNodePtr next = node->next; xmlUnlinkNode(node); xmlFreeNode(node); free(test_name); return next; } free(test_name); } } if (xmlStrEqual(node->name, BAD_CAST "p") || xmlStrEqual(node->name, BAD_CAST "td") || xmlStrEqual(node->name, BAD_CAST "pre")) { *list = rd_list_append(*list, node); } if (xmlStrEqual(node->name, BAD_CAST "div")) { if (should_alter_to_p(node)) { #ifdef READABLE_DEBUG char *debug_name = node_debug_name(node); DEBUG_LOG("Altering div %s to p \n", debug_name); free(debug_name); #endif xmlFree((void *)node->name); node->name = xmlCharStrdup("p"); *list = rd_list_append(*list, node); } } if (xmlStrEqual(node->name, BAD_CAST "script") || xmlStrEqual(node->name, BAD_CAST "noscript") || xmlStrEqual(node->name, BAD_CAST "style")) { htmlNodePtr next = node->next; xmlUnlinkNode(node); xmlFreeNode(node); return next; } if (node->type == XML_COMMENT_NODE) { htmlNodePtr next = node->next; xmlUnlinkNode(node); xmlFreeNode(node); return next; } if (xmlStrEqual(node->name, BAD_CAST "link")) { xmlChar *rel = xmlGetProp(node, BAD_CAST "rel"); if (rel) { |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |