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
|
#!/usr/bin/env perl
#
# Grab the Arduino reference documentation from the web site and
# modify the pages to create an offline reference.
#
# Author: Tom Pollard <tomp at earthlink dot net>
# Written: Jan 12, 2008
#
use strict;
use warnings;
print "username: "; my $user = <STDIN>; chomp($user);
print "password: "; my $pass = <STDIN>; chomp($pass);
my $verbose = 1;
my $CURL_OPTIONS = "--silent --show-error -u $user:$pass";
my $ARDUINO = 'http://edit.arduino.cc/en_ref'; # base url for reference site
my $PUBLIC = 'http://arduino.cc/en'; # base url for public site
my %downloaded = (); # keep track of the pages we download
my $guide = create_page('Guide_index.html', "$ARDUINO/Guide/HomePage");
my $faq = create_page('FAQ.html', "$ARDUINO/Main/FAQ");
my $env = create_page('environment.html', "$ARDUINO/Main/Environment");
my $css = create_page('arduinoUno.css', "$ARDUINO/pub/skins/arduinoUno/arduinoUno.css");
my $css2 = create_page('arduinoWide.css', "$ARDUINO/pub/skins/arduinoWide/arduinoWide.css");
my $css2 = create_page('arduinoWideRender.css', "$ARDUINO/pub/skins/arduinoWideRender/arduinoWideRender.css");
my $eeprom = create_page('EEPROM.html', "$ARDUINO/Reference/EEPROM");
my $stepper = create_page('Stepper.html', "$ARDUINO/Reference/Stepper");
my $softser = create_page('SoftwareSerial.html', "$ARDUINO/Reference/SoftwareSerial");
my $wire = create_page('Wire.html', "$ARDUINO/Reference/Wire");
my $sd = create_page('SD.html', "$ARDUINO/Reference/SD");
my $servo = create_page('Servo.html', "$ARDUINO/Reference/Servo");
my $spi = create_page('SPI.html', "$ARDUINO/Reference/SPI");
my $mousekeyboard = create_page('MouseKeyboard.html', "$ARDUINO/Reference/MouseKeyboard");
my $lcd = create_page('LiquidCrystal.html', "$ARDUINO/Reference/LiquidCrystal");
my $ethernet = create_page('Ethernet.html', "$ARDUINO/Reference/Ethernet");
my $serial = create_page('Serial.html', "$ARDUINO/Reference/Serial");
my $string = create_page('StringClass.html', "$ARDUINO/Reference/StringClass");
create_linked_pages($guide, qr!$ARDUINO/Guide/(\w+)!, 'Guide_%%.html');
create_linked_pages($softser, qr!$ARDUINO/Reference/(SoftwareSerial\w+)!, '%%.html');
create_linked_pages($eeprom, qr!$ARDUINO/Reference/(EEPROM\w+)!, '%%.html');
create_linked_pages($stepper, qr!$ARDUINO/Reference/(Stepper\w+)!, '%%.html');
create_linked_pages($wire, qr!$ARDUINO/Reference/(Wire\w+)!, '%%.html');
create_linked_pages($servo, qr!$ARDUINO/Reference/(Servo\w+)!, '%%.html');
create_linked_pages($sd, qr!$ARDUINO/Reference/(SD\w+)!, '%%.html');
create_linked_pages($sd, qr!$ARDUINO/Reference/(File\w+)!, '%%.html');
create_linked_pages($spi, qr!$ARDUINO/Reference/(SPI\w+)!, '%%.html');
create_linked_pages($mousekeyboard, qr!$ARDUINO/Reference/(Mouse\w+)!, '%%.html');
create_linked_pages($mousekeyboard, qr!$ARDUINO/Reference/(Keyboard\w+)!, '%%.html');
create_linked_pages($lcd, qr!$ARDUINO/Reference/(LiquidCrystal\w+)!, '%%.html');
create_linked_pages($ethernet, qr!$ARDUINO/Reference/(Ethernet\w+)!, '%%.html');
create_linked_pages($ethernet, qr!$ARDUINO/Reference/(Server\w+)!, '%%.html');
create_linked_pages($ethernet, qr!$ARDUINO/Reference/(Client\w+)!, '%%.html');
create_linked_pages($serial, qr!$ARDUINO/Serial/(\w+)!, 'Serial_%%.html');
create_linked_pages($string, qr!$ARDUINO/Reference/(String\w+)!, '%%.html');
my $index = create_page('index.html', "$ARDUINO/Reference/HomePage");
create_linked_pages($index, qr!$ARDUINO/Reference/(\w+)!, '%%.html');
#my $ext = create_page('Extended.html', "$ARDUINO/Reference/Extended");
#create_linked_pages($ext, qr!$ARDUINO/Reference/(\w+)!, '%%.html');
exit 0;
#------------------------- end of main code ----------------------------
########################################################################
# $original_text = create_page($filename, $url)
#
# Download the web page at the given URL, change links to point to
# the offline pages, and save it locally under the given filename.
# The original (unmodified) text of the downloaded page is returned.
#
sub create_page {
my $page = shift;
my $url = shift;
print "$page\n" if $verbose;
my $original_text = `curl $CURL_OPTIONS $url`;
die "** Unable to download $url **\n" if $? or ! $original_text;
$downloaded{$url} = $page; # remember that we downloaded this page
my $localized_text = localize_page($original_text);
open(my $PAGE, "> $page")
or die "** Unable to open $page for writing. **\n";
print $PAGE $localized_text;
close $PAGE;
return $original_text;
}
########################################################################
# $localized_text = localize_page($text)
#
# Rewrite links in the given text to point to the offline pages.
#
sub localize_page {
my $text = shift;
# replace links to unknown pages with links to '#'
$text =~ s!$ARDUINO/Reference/[^?"']*\?[^'"]*!#!xg;
# replace links to remote guide with links to local guide
$text =~ s!$ARDUINO/Guide/([^']+)!Guide_$1.html!xg;
# replace links to remote reference with links to local reference
$text =~ s!$ARDUINO/Reference/([^']*)!$1.html!xg;
# replace links to remove serial reference with links to local serial reference
$text =~ s!$ARDUINO/Serial/([^']*)!Serial_$1.html!xg;
# direct pages to the local style file
$text =~ s!$ARDUINO/pub/skins/arduinoUno/arduinoUno.css!arduinoUno.css!xg;
# direct pages to the local style file
$text =~ s!$ARDUINO/pub/skins/arduinoWide/arduinoWide.css!arduinoWide.css!xg;
# direct pages to the local style file
$text =~ s!$ARDUINO/pub/skins/arduinoWideRender/arduinoWideRender.css!arduinoWideRender.css!xg;
# change links to Main/FAQ to go to FAQ.html
$text =~ s!$ARDUINO/Main/FAQ!FAQ.html!xg;
# change links to the reference HomePage to go to index.html
$text =~ s!HomePage.html!index.html!xg;
# change links to the reference edit site to go to the public site
$text =~ s!$ARDUINO!$PUBLIC!xg;
# change links to the root directory to go to the Arduino home page
$text =~ s!href="/"!href="http://www.arduino.cc"/!xg;
return $text;
}
########################################################################
# create_linked_pages($text, $link_pattern, $page_name)
#
# Scan the given text for links matching the $link_pattern and
# create local files for the linked pages.
#
# The link_pattern is a regexp with one parenthesized subexpression -
# the text matching the subexpression will replace the
# special pattern '%%' in the $page_name to generate the name of
# the local file.
#
sub create_linked_pages {
my $text = shift;
my $link_pattern = shift;
my $new_name = shift;
while ($text =~ m!$link_pattern!g) {
my ($url, $name) = ($&, $1);
(my $page = $new_name) =~ s!%%!$name!;
next if $name =~ /\?/ || $downloaded{$url};
create_page($page, $url);
}
}
#---------------------------- end of code ------------------------------
|