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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
|
#!/usr/bin/perl
use 5.016;
use strict;
use warnings;
use Digest;
use Encode qw(encode_utf8);
use File::Temp qw(tempfile);
use Getopt::Long;
use List::Util qw(max min uniq);
use POSIX qw(strftime);
use Time::Piece;
use Time::Seconds;
use XML::LibXML;
use constant {
SELECTOR_STRING => 0,
SELECTOR_SINGLE_TEXT => 1,
SELECTOR_MULTI_TEXT => 2,
SELECTOR_SINGLE_HTML => 3,
SELECTOR_MULTI_HTML => 4,
};
my $USAGE = <<"HERE";
Usage:
html2rss.pl [options] config [location]
Options:
-a Generate atom feed
-r Generate RSS 2.0 feed
-o <file> Output feed to <file>
-h Print this usage message
HERE
my $NOW = localtime;
# TODO: If title is SELECTOR_STRING, make it unique somehow?
# TODO: Allow combining selector statements? like:
# SlackBuilds.org: [.//div[@class="whatever"]]
my $DEFAULT_AGENT = "html2rss.pl ($^O; perl $^V)";
my %DAY_MAP = (
0 => 'Sunday',
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
);
my %DAY_STRINGS = (
'0' => 0,
'7' => 0,
'sun' => 0,
'sunday' => 0,
'1' => 1,
'mon' => 1,
'monday' => 1,
'2' => 2,
'tue' => 2,
'tuesday' => 2,
'3' => 3,
'wed' => 3,
'wednesday' => 3,
'4' => 4,
'thu' => 4,
'thursday' => 4,
'5' => 5,
'fri' => 5,
'friday' => 5,
'6' => 6,
'sat' => 6,
'saturday' => 6,
);
my %FEED_FORMATS = map { $_ => 1 } qw(
atom
rss
);
sub read_config {
my ($file) = @_;
my $config = {};
open my $fh, '<', $file
or die "Failed to open $file for reading: $!\n";
my $section = undef;
my $ln = 0;
while (my $l = readline $fh) {
$ln++;
chomp $l;
if ($l =~ /^#/ or $l =~ /^\s*$/) {
next;
}
if ($l =~ /^\[(\w+)\]$/) {
$section = $1;
$config->{ $section } //= {};
next;
}
if (not defined $section) {
die "$file $ln: key-value pair not under section\n";
}
$l =~ s/^\s+|\s+$//g;
my ($key, $value) = split /\s*=\s*/, $l, 2;
if ($key !~ /^\w+$/) {
die "$file $ln: '$key' key contains invalid characters\n";
}
$config->{ $section }{ $key } = $value;
}
close $fh;
return $config;
}
sub curl_to {
my ($url, $to, %param) = @_;
my $agent = $param{ user_agent };
my @args;
if (defined $agent) {
push @args, '-A', $agent;
}
push @args, '-s', '--show-error';
push @args, '-o', $to;
push @args, $url;
system 'curl', @args;
if ($? == -1 or $? >> 8 != 0) {
die "Failed to curl '$url' to $to\n";
}
return 1;
}
sub id_selector {
my ($str) = @_;
if ($str =~ /^\[\[(.*)\]\]$/) {
return ($1, SELECTOR_MULTI_TEXT);
} elsif ($str =~ /^<<(.*)>>$/) {
return ($1, SELECTOR_MULTI_HTML);
} elsif ($str =~ /^\[(.*)\]$/) {
return ($1, SELECTOR_SINGLE_TEXT);
} elsif ($str =~ /^<(.*)>$/) {
return ($1, SELECTOR_SINGLE_HTML);
} else {
return ($str, SELECTOR_STRING);
}
}
sub selector_str_eq {
my ($selector, $string) = @_;
return 0 if $selector->[1] != SELECTOR_STRING;
return $selector->[0] eq $string;
}
sub truncate_week {
my ($time) = @_;
$time = $time->truncate(to => 'day');
while ($time->wday != 1) {
$time -= ONE_DAY;
}
return $time;
}
sub format_atom_time {
my ($time) = @_;
return strftime('%Y-%m-%dT%H:%M:%SZ', gmtime $time);
}
sub format_rss_time {
my ($time) = @_;
return strftime('%a, %d %b %Y %H:%M:%S -0000', gmtime $time);
}
sub generate_item_id {
my ($item, $salt) = @_;
my $sha = Digest->new('SHA-256');
if (defined $salt) {
$sha->add(encode_utf8($salt));
}
if (not defined $item->{ title }) {
die '$item missing title';
}
if (not defined $item->{ link }) {
die '$item missing link';
}
$sha->add(encode_utf8($item->{ title }));
$sha->add(encode_utf8($item->{ link }));
return $sha->hexdigest;
}
sub generate_feed_id {
my ($feed, $salt) = @_;
my $sha = Digest->new('SHA-256');
if (defined $salt) {
$sha->add(encode_utf8($salt));
}
if (not defined $feed->{ title }) {
die '$feed missing title';
}
if (not defined $feed->{ link }) {
die '$feed missing link';
}
$sha->add(encode_utf8($feed->{ title }));
$sha->add(encode_utf8($feed->{ link }));
return $sha->hexdigest;
}
sub generate_atom_item {
my ($item) = @_;
if (ref $item ne 'HASH') {
die '$item is not a hash ref';
}
my $node = XML::LibXML::Element->new('entry');
if (not defined $item->{ title }) {
die "item missing title\n";
}
my $title = $node->appendChild(
XML::LibXML::Element->new('title')
);
$title->appendChild(
XML::LibXML::Text->new($item->{ title })
);
if (not defined $item->{ link }) {
die "item missing link\n";
}
my $link = $node->appendChild(
XML::LibXML::Element->new('link')
);
$link->setAttribute('href', $item->{ link });
my $id = $node->appendChild(
XML::LibXML::Element->new('id')
);
$id->appendChild(
XML::LibXML::Text->new(generate_item_id($item))
);
if (defined $item->{ summary }) {
my $summary = $node->appendChild(
XML::LibXML::Element->new('summary')
);
$summary->setAttribute('type', $item->{ summary_html } ? 'html' : 'text');
$summary->appendChild(
XML::LibXML::Text->new($item->{ summary })
);
} else {
my $summary = $node->appendChild(
XML::LibXML::Element->new('summary')
);
$summary->setAttribute('type', 'text');
$summary->appendChild(
XML::LibXML::Text->new('')
);
}
if (defined $item->{ author }) {
my $author = $node->appendChild(
XML::LibXML::Element->new('author')
);
my $name = $author->appendChild(
XML::LibXML::Element->new('name')
);
$name->appendChild(
XML::LibXML::Text->new($item->{ author })
);
}
# TODO: Should this be mandatory?
if (defined $item->{ updated } or defined $item->{ published }) {
my $updated = $node->appendChild(
XML::LibXML::Element->new('updated')
);
$updated->appendChild(
XML::LibXML::Text->new(
format_atom_time($item->{ updated } // $item->{ published })
)
);
}
if (defined $item->{ categories }) {
for my $c (@{ $item->{ categories } }) {
my $cat = $node->appendChild(
XML::LibXML::Element->new('category')
);
$cat->setAttribute('term', $c);
}
}
if (defined $item->{ rights }) {
my $rights = $node->appendChild(
XML::LibXML::Element->new('rights')
);
$rights->appendChild(
XML::LibXML::Text->new($item->{ rights })
);
}
return $node;
}
sub generate_rss_item {
my ($item) = @_;
my $node = XML::LibXML::Element->new('item');
if (not defined $item->{ title }) {
die "item missing title\n";
}
if (not defined $item->{ link }) {
die "item missing link\n";
}
my $title = $node->appendChild(
XML::LibXML::Element->new('title')
);
$title->appendChild(
XML::LibXML::Text->new($item->{ title })
);
my $link = $node->appendChild(
XML::LibXML::Element->new('link')
);
$link->appendChild(
XML::LibXML::Text->new($item->{ link })
);
my $id = $node->appendChild(
XML::LibXML::Element->new('guid')
);
$id->appendChild(
XML::LibXML::Text->new(generate_item_id($item))
);
if (defined $item->{ published } or defined $item->{ updated }) {
my $pub = $node->appendChild(
XML::LibXML::Element->new('pubDate')
);
$pub->appendChild(
XML::LibXML::Text->new(
format_rss_time($item->{ published } // $item->{ updated })
)
);
}
if (defined $item->{ author }) {
my $author = $node->appendChild(
XML::LibXML::Element->new('author')
);
$author->appendChild(
XML::LibXML::Text->new($item->{ author })
);
}
if (defined $item->{ summary }) {
my $desc = $node->appendChild(
XML::LibXML::Element->new('description')
);
$desc->appendChild(
XML::LibXML::Text->new($item->{ summary })
);
}
if (defined $item->{ categories }) {
for my $c (@{ $item->{ categories } }) {
my $cat = $node->appendChild(
XML::LibXML::Element->new('category')
);
$cat->appendChild(
XML::LibXML::Text->new($c)
);
}
}
return $node;
}
sub generate_atom_feed {
my ($feed) = @_;
my $doc = XML::LibXML::Document->new();
my $node = XML::LibXML::Element->new('feed');
$node->setAttribute('xmlns', 'http://www.w3.org/2005/Atom');
$doc->setDocumentElement($node);
if (not defined $feed->{ title }) {
die "feed is missing title\n";
}
if (not defined $feed->{ link }) {
die "feed is missing link\n";
}
my $title = $node->appendChild(
XML::LibXML::Element->new('title')
);
$title->appendChild(
XML::LibXML::Text->new($feed->{ title })
);
my $link = $node->appendChild(
XML::LibXML::Element->new('link')
);
$link->setAttribute('href', $feed->{ link });
my $id = $node->appendChild(
XML::LibXML::Element->new('id')
);
$id->appendChild(
XML::LibXML::Text->new(generate_feed_id($feed))
);
# TODO: Updated/published should be mandatory
my $updated = $node->appendChild(
XML::LibXML::Element->new('updated')
);
$updated->appendChild(
XML::LibXML::Text->new(
format_atom_time($feed->{ updated } // $feed->{ published })
)
);
if (defined $feed->{ description }) {
my $desc = $node->appendChild(
XML::LibXML::Element->new('subtitle')
);
$desc->appendChild(
XML::LibXML::Text->new($feed->{ description })
);
}
if (defined $feed->{ rights }) {
my $rights = $node->appendChild(
XML::LibXML::Element->new('rights')
);
$rights->appendChild(
XML::LibXML::Text->new($feed->{ rights })
);
}
if (defined $feed->{ categories }) {
for my $c (@{ $feed->{ categories } }) {
my $cat = $node->appendChild(
XML::LibXML::Element->new('category')
);
$cat->setAttribute('term', $c);
}
}
if (defined $feed->{ generator }) {
my $gen = $node->appendChild(
XML::LibXML::Element->new('generator')
);
$gen->appendChild(
XML::LibXML::Text->new($feed->{ generator })
);
}
if (defined $feed->{ items }) {
for my $i (@{ $feed->{ items } }) {
my $item = generate_atom_item($i);
$node->appendChild($item);
}
}
return $doc;
}
sub generate_rss_feed {
my ($feed) = @_;
my $doc = XML::LibXML::Document->new;
my $node = XML::LibXML::Element->new('rss');
$node->setAttribute('version', '2.0');
$doc->setDocumentElement($node);
my $channel = $node->appendChild(
XML::LibXML::Element->new('channel')
);
if (not defined $feed->{ title }) {
die "feed is missing title\n";
}
if (not defined $feed->{ link }) {
die "feed is missing link\n";
}
my $title = $channel->appendChild(
XML::LibXML::Element->new('title')
);
$title->appendChild(
XML::LibXML::Text->new($feed->{ title })
);
my $link = $channel->appendChild(
XML::LibXML::Element->new('link')
);
$link->appendChild(
XML::LibXML::Text->new($feed->{ link })
);
my $desc = $channel->appendChild(
XML::LibXML::Element->new('description')
);
$desc->appendChild(
XML::LibXML::Text->new($feed->{ description } // '')
);
if (defined $feed->{ language }) {
my $lang = $channel->appendChild(
XML::LibXML::Element->new('language')
);
$lang->appendChild(
XML::LibXML::Text->new($feed->{ language })
);
}
if (defined $feed->{ rights }) {
my $rights = $channel->appendChild(
XML::LibXML::Element->new('copyright')
);
$rights->appendChild(
XML::LibXML::Text->new($feed->{ rights })
);
}
if (defined $feed->{ published }) {
my $pub = $channel->appendChild(
XML::LibXML::Element->new('pubDate')
);
$pub->appendChild(
XML::LibXML::Text->new(format_rss_time($feed->{ published }))
);
}
if (defined $feed->{ updated }) {
my $updated = $channel->appendChild(
XML::LibXML::Element->new('lastBuildDate')
);
$updated->appendChild(
XML::LibXML::Text->new(format_rss_time($feed->{ updated }))
);
}
if (defined $feed->{ categories }) {
for my $c (@{ $feed->{ categories } }) {
my $cat = $channel->appendChild(
XML::LibXML::Element->new('category')
);
$cat->appendChild(
XML::LibXML::Text->new($c)
);
}
}
if (defined $feed->{ generator }) {
my $gen = $channel->appendChild(
XML::LibXML::Element->new('generator')
);
$gen->appendChild(
XML::LibXML::Text->new($feed->{ generator })
);
}
if (defined $feed->{ ttl }) {
if ($feed->{ ttl } !~ /^\d+$/) {
die "channel ttl is not an integar\n";
}
my $ttl = $channel->appendChild(
XML::LibXML::Element->new('ttl')
);
$ttl->appendChild(
XML::LibXML::Text->new($feed->{ ttl })
);
}
if (defined $feed->{ skip_hours }) {
my $skip = $channel->appendChild(
XML::LibXML::Element->new('skipHours')
);
for my $s (uniq sort { $a <=> $b } @{ $feed->{ skip_hours } }) {
if ($s < 0 or $s > 23) {
die "skip_hours contains invalid value ($s)\n";
}
if ($s !~ /^\d+$/) {
die "skip_hours contains invalid value ($s)\n";
}
my $hour = $skip->appendChild(
XML::LibXML::Element->new('hour')
);
$hour->appendChild(
XML::LibXML::Text->new($s)
);
}
}
if (defined $feed->{ skip_days }) {
my $skip = $channel->appendChild(
XML::LibXML::Element->new('skipDays')
);
for my $s (uniq sort { $a <=> $b } @{ $feed->{ skip_days } }) {
if (not exists $DAY_MAP{ $s }) {
die "skip_days contains invalid value ($s)\n";
}
my $hour = $skip->appendChild(
XML::LibXML::Element->new('day')
);
$hour->appendChild(
XML::LibXML::Text->new($DAY_MAP{ $s })
);
}
}
if (defined $feed->{ items }) {
for my $i (@{ $feed->{ items } }) {
my $item = generate_rss_item($i);
$channel->appendChild($item);
}
}
return $doc;
}
sub process_config {
my ($file) = @_;
my $config = {
Feed => {
Title => undef,
Link => undef,
Description => undef,
Language => undef,
Rights => undef,
Published => undef,
Updated => undef,
Categories => undef,
Generator => undef,
TTL => undef,
SkipHours => undef,
SkipDays => undef,
Format => undef,
PublishedFmt => undef,
UpdatedFmt => undef,
},
Item => {
Select => undef,
Title => undef,
Link => undef,
Published => undef,
Updated => undef,
Author => undef,
Content => undef,
Categories => undef,
Rights => undef,
PublishedFmt => undef,
UpdatedFmt => undef,
},
};
my $hash = read_config($file);
if (not exists $hash->{ Feed }) {
die "$file missing [Feed] section\n";
}
if (not exists $hash->{ Item }) {
die "$file missing [Item] section\n";
}
# TODO: Are there sane defaults we can default to for these?
if (not exists $hash->{ Feed }{ Title }) {
die "$file missing [Feed].Title configuration\n";
}
if (not exists $hash->{ Feed }{ Link }) {
die "$file missing [Feed].Link configuration\n";
}
if (not exists $hash->{ Item }{ Select }) {
die "$file missing [Item].Select configuration\n";
}
if (not exists $hash->{ Item }{ Title }) {
die "$file missing [Item].Title configuration\n";
}
if (not exists $hash->{ Item }{ Link }) {
die "$file missing [Item].Link configuration\n";
}
$config->{ Feed }{ Title } = [ id_selector($hash->{ Feed }{ Title }) ];
$config->{ Feed }{ Link } = (id_selector($hash->{ Feed }{ Link }))[0];
$config->{ Item }{ Select } = (id_selector($hash->{ Item }{ Select }))[0];
$config->{ Item }{ Title } = [ id_selector($hash->{ Item }{ Title }) ];
$config->{ Item }{ Link } = [ id_selector($hash->{ Item }{ Link }) ];
if (exists $hash->{ Feed }{ Description }) {
$config->{ Feed }{ Description } = [
id_selector($hash->{ Feed }{ Description })
];
}
if (exists $hash->{ Feed }{ Language }) {
$config->{ Feed }{ Language } = [
id_selector($hash->{ Feed }{ Language })
];
}
if (exists $hash->{ Feed }{ Rights }) {
$config->{ Feed }{ Rights } = [
id_selector($hash->{ Feed }{ Rights })
];
}
if (exists $hash->{ Feed }{ Published }) {
$config->{ Feed }{ Published } = [
id_selector($hash->{ Feed }{ Published })
];
}
if (exists $hash->{ Feed }{ Updated }) {
$config->{ Feed }{ Updated } = [
id_selector($hash->{ Feed }{ Updated })
];
}
if (exists $hash->{ Feed }{ Categories }) {
$config->{ Feed }{ Categories } = [
id_selector($hash->{ Feed }{ Categories })
];
}
if (exists $hash->{ Feed }{ Generator }) {
$config->{ Feed }{ Generator } = [
id_selector($hash->{ Feed }{ Generator })
];
}
if (exists $hash->{ Feed }{ TTL }) {
if ($hash->{ Feed }{ TTL } !~ /^\d+$/) {
die "TTL must be an integar\n";
}
$config->{ Feed }{ TTL } = $hash->{ Feed }{ TTL };
}
if (exists $hash->{ Feed }{ SkipHours }) {
my @vals = split /\s*,\s*/, $hash->{ Feed }{ SkipHours };
for my $v (@vals) {
if ($v !~ /^\d+$/ or ($v < 0 or $v > 23)) {
die "SkipHours can only contain integars between 0 and 23\n";
}
push @{ $config->{ Feed }{ SkipHours } }, $v;
}
}
if (exists $hash->{ Feed }{ SkipDays }) {
my @vals = split /\s*,\s*/, $hash->{ Feed }{ SkipDays };
for my $v (@vals) {
my $vv = lc $v;
if (not exists $DAY_STRINGS{ $vv }) {
die "SkipHours contains invalid value ($v)\n";
}
push @{ $config->{ Feed }{ SkipDays } }, $DAY_STRINGS{ $vv };
}
}
if (exists $hash->{ Feed }{ Format }) {
$config->{ Feed }{ Format } = lc $hash->{ Feed }{ Format };
if (not $FEED_FORMATS{ $config->{ Feed }{ Format } }) {
die "'$hash->{ Feed }{ Format }' is not a valid feed format\n";
}
} else {
$config->{ Feed }{ Format } = 'atom';
}
if (exists $hash->{ Feed }{ PublishedFmt }) {
$config->{ Feed }{ PublishedFmt } = $hash->{ Feed }{ PublishedFmt };
}
if (exists $hash->{ Feed }{ UpdatedFmt }) {
$config->{ Feed }{ UpdatedFmt } = $hash->{ Feed }{ UpdatedFmt };
}
if (exists $hash->{ Item }{ Published }) {
$config->{ Item }{ Published } = [
id_selector($hash->{ Item }{ Published })
];
}
if (exists $hash->{ Item }{ Updated }) {
$config->{ Item }{ Updated } = [
id_selector($hash->{ Item }{ Updated })
];
}
if (exists $hash->{ Item }{ Author }) {
$config->{ Item }{ Author } = [
id_selector($hash->{ Item }{ Author })
];
}
if (exists $hash->{ Item }{ Content }) {
$config->{ Item }{ Content } = [
id_selector($hash->{ Item }{ Content })
];
}
if (exists $hash->{ Item }{ Categories }) {
$config->{ Item }{ Categories } = [
id_selector($hash->{ Item }{ Categories })
];
}
if (exists $hash->{ Item }{ Rights }) {
$config->{ Item }{ Rights } = [
id_selector($hash->{ Item }{ Rights })
];
}
if (exists $hash->{ Item }{ PublishedFmt }) {
$config->{ Item }{ PublishedFmt } = $hash->{ Item }{ PublishedFmt };
}
if (exists $hash->{ Item }{ UpdatedFmt }) {
$config->{ Item }{ UpdatedFmt } = $hash->{ Item }{ UpdatedFmt };
}
if (defined $config->{ Feed }{ Published }) {
if (
$config->{ Feed }{ Published }[0] eq 'updated' and
$config->{ Feed }{ Published }[1] == SELECTOR_STRING
) {
if (not defined $config->{ Feed }{ Updated }) {
die "published set to 'updated', but 'updated' not defined\n";
}
if (
$config->{ Feed }{ Updated }[0] eq 'published' and
$config->{ Feed }{ Published }[1] == SELECTOR_STRING
) {
die "published cannot be set to 'updated' if updated is set to 'published'\n";
}
}
if (
$config->{ Feed }{ Published }[0] eq 'published' and
$config->{ Feed }{ Published }[1] == SELECTOR_STRING
) {
die "published cannot be set to 'published'\n";
}
}
if (defined $config->{ Feed }{ Updated }) {
if (
$config->{ Feed }{ Updated }[0] eq 'updated' and
$config->{ Feed }{ Updated }[1] == SELECTOR_STRING
) {
die "updated cannot be set to 'updated'\n";
}
}
return $config;
}
sub xpath_select {
my ($node, $selector) = @_;
if ($selector->[1] == SELECTOR_STRING) {
return $selector->[0];
} elsif ($selector->[1] == SELECTOR_SINGLE_TEXT) {
my ($n) = $node->findnodes($selector->[0]);
if (not defined $n) {
die "Nothing matches '$selector->[0]'\n";
}
if ($n->isa('XML::LibXML::Attr')) {
return $n->value // '';
}
return $n->textContent;
} elsif ($selector->[1] == SELECTOR_MULTI_TEXT) {
my @found = $node->findnodes($selector->[0]);
if (!@found) {
die "Nothing matches '$selector->[0]'\n";
}
my $text = '';
for my $n (@found) {
if ($n->isa('XML::LibXML::Attr')) {
$text .= $n->value // '';
} else {
$text .= $n->textContent;
}
}
return $text;
} elsif ($selector->[1] == SELECTOR_SINGLE_HTML) {
my ($n) = $node->findnodes($selector->[0]);
if (not defined $n) {
die "Nothing matches '$selector->[0]'\n";
}
if ($n->isa('XML::LibXML::Attr')) {
die "Cannot select the HTML of an attribute node\n";
}
return $n->toString;
} elsif ($selector->[1] == SELECTOR_MULTI_HTML) {
my @found = $node->findnodes($selector->[1]);
if (!@found) {
die "Nothing matches '$selector->[0]'\n";
}
my $text = '';
for my $n (@found) {
if ($n->isa('XML::LibXML::Attr')) {
die "Cannot select the HTML of an attribute node\n";
}
$text .= $n->toString;
}
return $text;
}
die "Invalid selector type";
}
sub xpath_multi_select {
my ($dom, $selector) = @_;
if ($selector->[1] == SELECTOR_STRING) {
return ($selector->[0]);
} elsif ($selector->[1] == SELECTOR_SINGLE_TEXT or $selector->[1] == SELECTOR_MULTI_TEXT) {
my @found = $dom->findnodes($selector->[0]);
return map {
$_->isa('XML::LibXML::Attr') ? $_->value // '' : $_->textContent
} @found;
} elsif ($selector->[1] == SELECTOR_SINGLE_HTML or $selector->[1] == SELECTOR_MULTI_HTML) {
my @found = $dom->findnodes($selector->[1]);
@found = grep { !$_->isa('XML::LibXML::Attr') } @found;
return map { $_->toString } @found;
}
die "Invalid selector type";
}
sub html2feed {
my ($html, $config) = @_;
my $feed = {
title => undef,
link => undef,
updated => undef,
published => undef,
description => undef,
language => undef,
rights => undef,
categories => undef,
generator => undef,
ttl => undef,
skip_hours => undef,
skip_days => undef,
items => [],
};
my $dom = XML::LibXML->load_html(
location => $html,
recover => 2,
);
$feed->{ title } = xpath_select($dom, $config->{ Feed }{ Title });
$feed->{ link } = $config->{ Feed }{ Link };
if (defined $config->{ Feed }{ Published }) {
if ($config->{ Feed }{ Published }[1] == SELECTOR_STRING) {
my $p = $config->{ Feed }{ Published }[0];
if ($p eq 'now') {
$feed->{ published } = $NOW->epoch;
} elsif ($p eq 'today') {
$feed->{ published } = $NOW->truncate(to => 'day')->epoch;
} elsif ($p eq 'week') {
$feed->{ published } = truncate_week($NOW)->epoch;
} elsif ($p eq 'month') {
$feed->{ published } = $NOW->truncate(to => 'month')->epoch;
} elsif ($p eq 'year') {
$feed->{ published } = $NOW->truncate(to => 'year')->epoch;
} elsif ($p eq 'updated') {
if (not defined $config->{ Feed }{ Updated }) {
die "Cannot set published to 'updated': [Feed].Updated not configured\n";
}
# Set this later...
} elsif ($p eq 'published') {
die "Cannot set published to 'published'\n";
} elsif (defined $config->{ Feed }{ PublishedFmt }) {
my $t = Time::Piece->strptime($p, $config->{ Feed }{ PublishedFmt });
$feed->{ published } = $t->epoch;
} else {
die "Cannot set [Feed].Published to a string without setting PublishedFmt\n";
}
} else {
if (not defined $config->{ Feed }{ PublishedFmt }) {
die "Cannot determine publish time; [Feed].PublishedFmt not configured\n";
}
my $sel = xpath_select($dom, $config->{ Feed }{ Published });
my $t = Time::Piece->strptime($sel, $config->{ Feed }{ PublishedFmt });
$feed->{ published } = $t->epoch;
}
}
if (defined $config->{ Feed }{ Updated }) {
if ($config->{ Feed }{ Updated }[1] == SELECTOR_STRING) {
my $p = $config->{ Feed }{ Updated }[0];
if ($p eq 'now') {
$feed->{ updated } = $NOW->epoch;
} elsif ($p eq 'today') {
$feed->{ updated } = $NOW->truncate(to => 'day')->epoch;
} elsif ($p eq 'week') {
$feed->{ updated } = truncate_week($NOW)->epoch;
} elsif ($p eq 'month') {
$feed->{ updated } = $NOW->truncate(to => 'month')->epoch;
} elsif ($p eq 'year') {
$feed->{ updated } = $NOW->truncate(to => 'year')->epoch;
} elsif ($p eq 'updated') {
die "Cannot set updated to 'updated'\n";
} elsif ($p eq 'published') {
if (not defined $feed->{ published }) {
die "Cannot set updated to 'published': published is not set\n";
}
$feed->{ updated } = $feed->{ published };
} elsif (defined $config->{ Feed }{ UpdatedFmt }) {
my $t = Time::Piece->strptime($p, $config->{ Feed }{ UpdatedFmt });
$feed->{ updated } = $t->epoch;
} else {
die "Cannot set [Feed].Updated to a string without setting UpdatedFmt\n";
}
} else {
if (not defined $config->{ Feed }{ UpdatedFmt }) {
die "Cannot determine publish time; [Feed].UpdatedFmt not configured\n";
}
my $sel = xpath_select($dom, $config->{ Feed }{ Updated });
my $t = Time::Piece->strptime($sel, $config->{ Feed }{ UpdatedFmt });
$feed->{ updated } = $t->epoch;
}
}
if (
defined $config->{ Feed }{ Published } and
$config->{ Feed }{ Published }[1] == SELECTOR_STRING and
$config->{ Feed }{ Published }[0] eq 'updated'
) {
$feed->{ published } = $feed->{ updated };
}
if (defined $config->{ Feed }{ Description }) {
$feed->{ description } = xpath_select($dom, $config->{ Feed }{ Description });
}
if (defined $config->{ Feed }{ Language }) {
$feed->{ language } = xpath_select($dom, $config->{ Feed }{ Language });
}
if (defined $config->{ Feed }{ Rights }) {
$feed->{ Rights } = xpath_select($dom, $config->{ Feed }{ Rights });
}
if (defined $config->{ Feed }{ Categories }) {
if ($config->{ Feed }{ Categories }[1] == SELECTOR_STRING) {
my @cats = split /\s*,\s*/, $config->{ Feed }{ Categories }[0];
$feed->{ categories } = \@cats;
} else {
$feed->{ categories } = [
xpath_multi_select($dom, $config->{ Feed }{ Categories })
];
}
}
if (defined $config->{ Feed }{ Generator }) {
$feed->{ generator } = xpath_select($dom, $config->{ Feed }{ Generator });
}
if (defined $config->{ Feed }{ TTL }) {
$feed->{ ttl } = $config->{ Feed }{ TTL };
}
if (defined $config->{ Feed }{ SkipHours }) {
$feed->{ skip_hours } = $config->{ Feed }{ SkipHours };
}
if (defined $config->{ Feed }{ SkipDays }) {
$feed->{ skip_days } = $config->{ Feed }{ SkipDays };
}
my @found = $dom->findnodes($config->{ Item }{ Select });
for my $n (@found) {
my $item = {
title => undef,
link => undef,
published => undef,
updated => undef,
author => undef,
summary => undef,
summary_html => 0,
categories => undef,
rights => undef,
};
$item->{ title } = xpath_select($n, $config->{ Item }{ Title });
$item->{ link } = xpath_select($n, $config->{ Item }{ Link });
if (defined $config->{ Item }{ Published }) {
if ($config->{ Item }{ Published }[1] == SELECTOR_STRING) {
my $p = $config->{ Item }{ Published }[0];
if ($p eq 'now') {
$item->{ published } = $NOW->epoch;
} elsif ($p eq 'today') {
$item->{ published } = $NOW->truncate(to => 'day')->epoch;
} elsif ($p eq 'week') {
$item->{ published } = truncate_week($NOW)->epoch;
} elsif ($p eq 'month') {
$item->{ published } = $NOW->truncate(to => 'month')->epoch;
} elsif ($p eq 'year') {
$item->{ published } = $NOW->truncate(to => 'year')->epoch;
} elsif ($p eq 'updated') {
if (not defined $config->{ Item }{ Updated }) {
die "Cannot set published to 'updated': [Item].Updated not set\n";
}
# Set this later...
} elsif ($p eq 'published') {
die "Cannot set published to 'published'\n";
} elsif (defined $config->{ Item }{ PublishedFmt }) {
my $t = Time::Piece->strptime($p, $config->{ Item }{ PublishedFmt });
$item->{ published } = $t->epoch;
} else {
die "Cannot set [Item].Published to string without setting PublishedFmt\n";
}
} else {
if (not defined $config->{ Item }{ PublishedFmt }) {
die "Cannot determine publish time; [Item].PublishedFmt not configured\n";
}
my $sel = xpath_select($dom, $config->{ Item }{ Published });
my $t = Time::Piece->strptime($sel, $config->{ Item }{ PublishedFmt });
$item->{ published } = $t->epoch;
}
}
if (defined $config->{ Item }{ Updated }) {
if ($config->{ Item }{ Updated }[1] == SELECTOR_STRING) {
my $p = $config->{ Item }{ Updated }[0];
if ($p eq 'now') {
$item->{ updated } = $NOW->epoch;
} elsif ($p eq 'today') {
$item->{ updated } = $NOW->truncate(to => 'day')->epoch;
} elsif ($p eq 'week') {
$item->{ updated } = truncate_week($NOW)->epoch;
} elsif ($p eq 'month') {
$item->{ updated } = $NOW->truncate(to => 'month')->epoch;
} elsif ($p eq 'year') {
$item->{ updated } = $NOW->truncate(to => 'year')->epoch;
} elsif ($p eq 'updated') {
die "Cannot set updated to 'updated'\n";
} elsif ($p eq 'published') {
if (not defined $item->{ published }) {
die "Cannot set updated to 'published': [Item].published is not set\n";
}
$item->{ updated } = $item->{ published };
} elsif (defined $config->{ Item }{ UpdatedFmt }) {
my $t = Time::Piece->strptime($p, $config->{ Item }{ UpdatedFmt });
$item->{ updated } = $t->epoch;
} else {
die "Cannot set [Item].Updated to string without setting UpdatedFmt\n";
}
} else {
if (not defined $config->{ Item }{ UpdatedFmt }) {
die "Cannot determine publish time; [Item].UpdatedFmt not configured\n";
}
my $sel = xpath_select($dom, $config->{ Item }{ Updated });
my $t = Time::Piece->strptime($sel, $config->{ Item }{ UpdatedFmt });
$item->{ updated } = $t->epoch;
}
}
if (
defined $config->{ Item }{ Published } and
$config->{ Item }{ Published }[1] == SELECTOR_STRING and
$config->{ Item }{ Published }[0] eq 'updated'
) {
$item->{ published } = $item->{ updated };
}
if (defined $config->{ Item }{ Author }) {
$item->{ author } = xpath_select($n, $config->{ Item }{ Author });
}
if (defined $config->{ Item }{ Content }) {
$item->{ summary } = xpath_select($n, $config->{ Item }{ Content });
if (
$config->{ Item }{ Content }->[1] == SELECTOR_SINGLE_HTML or
$config->{ Item }{ Content } == SELECTOR_MULTI_HTML
) {
$item->{ summary_html } = 1;
}
}
if (defined $config->{ Item }{ Categories }) {
if ($config->{ Item }{ Categories }[1] == SELECTOR_STRING) {
my @cats = split /\s*,\s*/, $config->{ Item }{ Categories }[0];
$item->{ categories } = \@cats;
} else {
$item->{ categories } = [
xpath_multi_select($n, $config->{ Item }{ Categories })
];
}
}
if (defined $config->{ Item }{ Rights }) {
$item->{ rights } = xpath_select($n, $config->{ Item }{ Rights });
}
push @{ $feed->{ items } }, $item;
}
my @times =
grep { defined }
map { @{ $_ }{ qw(published updated) } }
@{ $feed->{ items } };
if (not defined $feed->{ published }) {
$feed->{ published } = min @times;
}
if (not defined $feed->{ updated }) {
$feed->{ updated } = max @times;
}
return $feed;
}
GetOptions(
'a' => \my $force_atom,
'r' => \my $force_rss,
'o=s' => \my $output,
'h' => sub { print $USAGE; exit 0 },
) or die $USAGE;
my ($config_file, $html) = @ARGV;
if (not defined $config_file) {
die $USAGE;
}
my $config = process_config($config_file);
if (not defined $html) {
$html = $config->{ Feed }{ Link };
if (not defined $html) {
die "html2rss.pl requires an HTML file/page supplied to it either via a command-line argument or the [Feed].Link configuration field\n";
}
}
my $file;
if ($html =~ /^\w+:\/\//) {
my $tmp = do {
my ($h, $p) = tempfile(UNLINK => 1);
close $h;
$p;
};
curl_to($html, $tmp, user_agent => $DEFAULT_AGENT);
$file = $tmp;
} else {
$file = $html;
}
my $feed = html2feed($file, $config);
my $feed_xml;
if ($force_atom) {
$feed_xml = generate_atom_feed($feed);
} elsif ($force_rss) {
$feed_xml = generate_rss_feed($feed);
} elsif (defined $config->{ Feed }{ Format }) {
if ($config->{ Feed }{ Format } eq 'atom') {
$feed_xml = generate_atom_feed($feed);
} elsif ($config->{ Feed }{ Format } eq 'rss') {
$feed_xml = generate_rss_feed($feed);
} else {
die "'$config->{ Feed }{ Format }' is not a valid feed format\n";
}
} else {
$feed_xml = generate_atom_feed($feed);
}
if (not defined $output) {
binmode *STDOUT;
$feed_xml->toFH(*STDOUT, 2);
} else {
$feed_xml->toFile($output, 2);
say "Wrote feed to $output";
}
=head1 NAME
html2rss.pl - Convert HTML pages to RSS feeds
=head1 USAGE
html2rss.pl [options] config [location]
=head1 DESCRIPTION
B<html2rss.pl> is a Perl script that converts HTML pages to RSS feeds based on
parameters read from a given feed configuration file.
B<html2rss.pl> takes a configuration file as input, whose format is described
in the subsequent section. B<html2rss.pl> can also optionally take either a
path to an HTML file or a URL to manually specify the HTML file/page to
convert. If no location is given, B<html2rss.pl> will convert the URL/path
set as the feed link in the configuration file.
=head1 CONFIGURATION
B<html2rss.pl> processes HTML pages based on parameters it reads from
configuration files. B<html2rss.pl> uses a configuration format similar to
the INI file format. A configuration file consists of sections marked with
their name enclosed in square brackets.
# Section named "Feed"
[Feed]
Sections contain lists of configuration options which are lines of
key-value pairs that are seperated by an equals sign.
[Feed]
Title = Yadda yadda...
Link = https://phony.com/
Description = [/html/body/div[1]/p]
Lines starting with a hash are comments and will be ignored by
B<html2rss.pl>.
B<html2rss.pl> requires two sections: C<[Feed]> and C<[Item]>.
=head2 Selectors
Many configuration options can take an XPath selector statement as a value. A
selector statement will select elements from the HTML's DOM tree and use the
captured text as the value for the configuration field. Selector statements
are enclosed in either square or angle brackets. A single pair of brackets
will capture only the first selected element. A double pair of brackets will
capture the contents of all matching elements.
=over 4
=item [I<selector>]
=item [[I<selector>]]
Capture the text contents of the selected elements.
=item <I<selector>>
=item <<I<selector>>>
Capture the serialized HTML of the selected elements.
=back
=head2 [Feed] options
=over 4
=item Title = I<string> | I<selector>
The string or element selector for the feed's title. B<This is a required
field>.
=item Link = I<url>
The HTML page's URL. B<This is a required field>. B<This is a required field>.
=item Description = I<string> | I<selector>
The string or element selector for the feed's description.
=item Language = I<string> | I<selector>
The string or element selector for the feed's language.
=item Rights = I<string> | I<selector>
The string or element selector for the feed's rights disclaimer.
=item Published = I<time> | I<selector>
The string or element selector for the feed's published date. See the
L</Special Time Options> section below for a list of special string options that
can be used for this field. If this field is set to a non-special string or
selector, B<html2rss.pl> will read the string/captured text as a timestamp in
the format specified by C<PublishedFmt>. If C<PublishedFmt> is not set, an
error will be raised.
=item Updated = I<time> | I<selector>
Same as C<Published>, but for a post's update time.
=item Categories = I<tags> | I<selector>
Comma-seperated list of tags or element selector for a feed's category list.
If a selector is used, the selector will use the captured contents of each
element (regardless of whether it is a single selector or multi selector) as
seperate categories.
=item Generator = I<string> | I<selector>
The string or element for the feed's generator.
=item TTL = I<minutes>
Number of minutes to use for the feed's TTL.
=item SkipHours = I<hours>
Comma-seperated list of hours (C<0>-C<23>) to use for the feed's
C<skipHours> field.
=item SkipDays = I<days>
Comma-seperated list of days to use for the feed's C<skipDays> field. A day can
be a week day's numerical value (C<0> for Sunday, C<1> for Monday, ...), the
abbreviated name of the week day (C<sun>, C<mon>, ...), or the week day's full
name (C<sunday>, C<monday>, ...). Case does not matter.
=item Format = I<format>
Default format to use for the generated feed. Can either be C<atom> or C<rss>.
C<atom> is the default.
=item PublishedFmt = I<fmt>
The L<striptime(3)> format string to use for parsing C<Published> timestamps.
=item UpdatedFmt = I<fmt>
Same as above, but for C<Updated> timestamps.
=back
=head2 [Item] options
=over 4
=item Select = I<selector>
Element selector that will be used to select each element that will be
converted into a feed entry. Each selected element will be used as the base
element for other C<[Item]> selector statements, so that you can use the
C<./...> syntax to select relative to that element. B<This is a required
field>.
=item Title = I<string> | I<selector>
The string or element selector for the item's title. B<This is a required
field>.
=item Link = I<url> | I<selector>
The URL or element selector for the item's link. B<This is a required field>.
=item Published = I<time> | I<selector>
The string or element selector for the item's published date. See the
L</Special Time Options> section below for a list of special string options that
can be used for this field. If this field is set to a non-special string or
selector, B<html2rss.pl> will read the string/captured text as a timestamp in
the format specified by C<PublishedFmt>. If C<PublishedFmt> is not set, an
error will be raised.
=item Updated = I<time> | I<selector>
Same as above, but for the C<Updated> field.
=item Author = I<string> | I<selector>
The string or element selector for the item's author.
=item Content = I<string> | I<selector>
The string or element selector for the item's content.
=item Categories = I<tags> | I<selector>
Comma-seperated list or element selector for the item's category list.
If a selector is used, the selector will use the captured contents of each
element (regardless of whether it is a single selector or multi selector) as
seperate categories.
=item Rights = I<string> | I<selector>
The string or element selector for the item's rights.
=item PublishedFmt = I<fmt>
The L<strftime(3)> format to use for timestamps parsed in the C<Published>
field.
=item UpdatedFmt = I<fmt>
The same as above, but for the C<Updated> field.
=back
=head3 Special Time Options
=over 4
=item now
Set time to time the script was ran.
=item today
Set time to the beginning of today.
=item week
Set time to the beginning of the week.
=item month
Set time to the beginning of the month.
=item year
Set time to the beginning of the year.
=item published
Set time to the time of the feed/item's C<Published> field. Only works for the
C<Updated> fields.
=item updated
Set time to the time of the feed/item's C<Updated> field. Only works for the
C<Published> fields.
=back
=head1 OPTIONS
=over 4
=item B<-a>
Generate an Atom feed.
=item B<-r>
Generate an RSS 2.0 feed.
=item B<-o> I<file>
Output the generated feed to the specified file. If not set, the feed will
be written to standard output.
=item B<-h>
Print the script's usage message.
=back
=head1 AUTHOR
Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
This project's source can be found on its
L<Codeberg page|https://codeberg.org/1-1sam/noss.git>. Comments and pull
requests are welcome!
=head1 COPYRIGHT
Copyright (C) 2025 Samuel Young
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
=head1 SEE ALSO
L<noss(1)>, L<strftime(3)>
=cut
|