| 1 |
#!/usr/bin/env perl |
| 2 |
# |
| 3 |
# Grab the Arduino reference documentation from the web site and |
| 4 |
# modify the pages to create an offline reference. |
| 5 |
# |
| 6 |
# Author: Tom Pollard <tomp at earthlink dot net> |
| 7 |
# Written: Jan 12, 2008 |
| 8 |
# |
| 9 |
use strict; |
| 10 |
use warnings; |
| 11 |
|
| 12 |
print "username: "; my $user = <STDIN>; chomp($user); |
| 13 |
print "password: "; my $pass = <STDIN>; chomp($pass); |
| 14 |
|
| 15 |
my $verbose = 1; |
| 16 |
my $CURL_OPTIONS = "--silent --show-error -u $user:$pass"; |
| 17 |
|
| 18 |
my $ARDUINO = 'http://edit.arduino.cc/en_ref'; # base url for reference site |
| 19 |
my $PUBLIC = 'http://www.arduino.cc/en'; # base url for public site |
| 20 |
|
| 21 |
my %downloaded = (); # keep track of the pages we download |
| 22 |
|
| 23 |
my $guide = create_page('Guide_index.html', "$ARDUINO/Guide/HomePage"); |
| 24 |
|
| 25 |
my $faq = create_page('FAQ.html', "$ARDUINO/Main/FAQ"); |
| 26 |
my $env = create_page('environment.html', "$ARDUINO/Main/Environment"); |
| 27 |
my $css = create_page('arduinoUno.css', "$ARDUINO/pub/skins/arduinoUno/arduinoUno.css"); |
| 28 |
my $css2 = create_page('arduinoWide.css', "$ARDUINO/pub/skins/arduinoWide/arduinoWide.css"); |
| 29 |
my $css3 = create_page('arduinoWideRender.css', "$ARDUINO/pub/skins/arduinoWideRender/arduinoWideRender.css"); |
| 30 |
my $eeprom = create_page('EEPROM.html', "$ARDUINO/Reference/EEPROM"); |
| 31 |
my $stepper = create_page('Stepper.html', "$ARDUINO/Reference/Stepper"); |
| 32 |
my $softser = create_page('SoftwareSerial.html', "$ARDUINO/Reference/SoftwareSerial"); |
| 33 |
my $wire = create_page('Wire.html', "$ARDUINO/Reference/Wire"); |
| 34 |
my $sd = create_page('SD.html', "$ARDUINO/Reference/SD"); |
| 35 |
my $servo = create_page('Servo.html', "$ARDUINO/Reference/Servo"); |
| 36 |
my $spi = create_page('SPI.html', "$ARDUINO/Reference/SPI"); |
| 37 |
my $mousekeyboard = create_page('MouseKeyboard.html', "$ARDUINO/Reference/MouseKeyboard"); |
| 38 |
my $lcd = create_page('LiquidCrystal.html', "$ARDUINO/Reference/LiquidCrystal"); |
| 39 |
my $ethernet = create_page('Ethernet.html', "$ARDUINO/Reference/Ethernet"); |
| 40 |
my $serial = create_page('Serial.html', "$ARDUINO/Reference/Serial"); |
| 41 |
my $stream = create_page('Stream.html', "$ARDUINO/Reference/Stream"); |
| 42 |
my $string = create_page('StringObject.html', "$ARDUINO/Reference/StringObject"); |
| 43 |
|
| 44 |
create_linked_pages($guide, qr!$ARDUINO/Guide/(\w+)!, 'Guide_%%.html'); |
| 45 |
create_linked_pages($softser, qr!$ARDUINO/Reference/(SoftwareSerial\w+)!, '%%.html'); |
| 46 |
create_linked_pages($eeprom, qr!$ARDUINO/Reference/(EEPROM\w+)!, '%%.html'); |
| 47 |
create_linked_pages($stepper, qr!$ARDUINO/Reference/(Stepper\w+)!, '%%.html'); |
| 48 |
create_linked_pages($wire, qr!$ARDUINO/Reference/(Wire\w+)!, '%%.html'); |
| 49 |
create_linked_pages($servo, qr!$ARDUINO/Reference/(Servo\w+)!, '%%.html'); |
| 50 |
create_linked_pages($sd, qr!$ARDUINO/Reference/(SD\w+)!, '%%.html'); |
| 51 |
create_linked_pages($sd, qr!$ARDUINO/Reference/(File\w+)!, '%%.html'); |
| 52 |
create_linked_pages($spi, qr!$ARDUINO/Reference/(SPI\w+)!, '%%.html'); |
| 53 |
create_linked_pages($mousekeyboard, qr!$ARDUINO/Reference/(Mouse\w+)!, '%%.html'); |
| 54 |
create_linked_pages($mousekeyboard, qr!$ARDUINO/Reference/(Keyboard\w+)!, '%%.html'); |
| 55 |
create_linked_pages($lcd, qr!$ARDUINO/Reference/(LiquidCrystal\w+)!, '%%.html'); |
| 56 |
create_linked_pages($ethernet, qr!$ARDUINO/Reference/(Ethernet\w+)!, '%%.html'); |
| 57 |
create_linked_pages($ethernet, qr!$ARDUINO/Reference/(Server\w+)!, '%%.html'); |
| 58 |
create_linked_pages($ethernet, qr!$ARDUINO/Reference/(Client\w+)!, '%%.html'); |
| 59 |
create_linked_pages($serial, qr!$ARDUINO/Serial/(\w+)!, 'Serial_%%.html'); |
| 60 |
create_linked_pages($string, qr!$ARDUINO/Reference/(String\w+)!, '%%.html'); |
| 61 |
create_linked_pages($stream, qr!$ARDUINO/Reference/(Stream\w+)!, '%%.html'); |
| 62 |
create_linked_pages($string, qr!$ARDUINO/Reference/(String\w+)!, '%%.html'); |
| 63 |
|
| 64 |
my $index = create_page('index.html', "$ARDUINO/Reference/HomePage"); |
| 65 |
|
| 66 |
create_linked_pages($index, qr!$ARDUINO/Reference/(\w+)!, '%%.html'); |
| 67 |
|
| 68 |
#my $ext = create_page('Extended.html', "$ARDUINO/Reference/Extended"); |
| 69 |
|
| 70 |
#create_linked_pages($ext, qr!$ARDUINO/Reference/(\w+)!, '%%.html'); |
| 71 |
|
| 72 |
exit 0; |
| 73 |
|
| 74 |
#------------------------- end of main code ---------------------------- |
| 75 |
|
| 76 |
######################################################################## |
| 77 |
# $original_text = create_page($filename, $url) |
| 78 |
# |
| 79 |
# Download the web page at the given URL, change links to point to |
| 80 |
# the offline pages, and save it locally under the given filename. |
| 81 |
# The original (unmodified) text of the downloaded page is returned. |
| 82 |
# |
| 83 |
sub create_page { |
| 84 |
my $page = shift; |
| 85 |
my $url = shift; |
| 86 |
|
| 87 |
print "$page\n" if $verbose; |
| 88 |
my $original_text = `curl $CURL_OPTIONS $url`; |
| 89 |
die "** Unable to download $url **\n" if $? or ! $original_text; |
| 90 |
$downloaded{$url} = $page; # remember that we downloaded this page |
| 91 |
|
| 92 |
my $localized_text = localize_page($original_text); |
| 93 |
open(my $PAGE, "> $page") |
| 94 |
or die "** Unable to open $page for writing. **\n"; |
| 95 |
print $PAGE $localized_text; |
| 96 |
close $PAGE; |
| 97 |
|
| 98 |
return $original_text; |
| 99 |
} |
| 100 |
|
| 101 |
######################################################################## |
| 102 |
# $localized_text = localize_page($text) |
| 103 |
# |
| 104 |
# Rewrite links in the given text to point to the offline pages. |
| 105 |
# |
| 106 |
sub localize_page { |
| 107 |
my $text = shift; |
| 108 |
|
| 109 |
# replace links to unknown pages with links to '#' |
| 110 |
$text =~ s!$ARDUINO/Reference/[^?"']*\?[^'"]*!#!xg; |
| 111 |
|
| 112 |
# replace links to remote guide with links to local guide |
| 113 |
$text =~ s!$ARDUINO/Guide/([^']+)!Guide_$1.html!xg; |
| 114 |
|
| 115 |
# replace links to remote reference with links to local reference |
| 116 |
$text =~ s!$ARDUINO/Reference/([^']*)!$1.html!xg; |
| 117 |
|
| 118 |
# replace links to remove serial reference with links to local serial reference |
| 119 |
$text =~ s!$ARDUINO/Serial/([^']*)!Serial_$1.html!xg; |
| 120 |
|
| 121 |
# direct pages to the local style file |
| 122 |
$text =~ s!$ARDUINO/pub/skins/arduinoUno/arduinoUno.css!arduinoUno.css!xg; |
| 123 |
|
| 124 |
# direct pages to the local style file |
| 125 |
$text =~ s!$ARDUINO/pub/skins/arduinoWide/arduinoWide.css!arduinoWide.css!xg; |
| 126 |
|
| 127 |
# direct pages to the local style file |
| 128 |
$text =~ s!$ARDUINO/pub/skins/arduinoWideRender/arduinoWideRender.css!arduinoWideRender.css!xg; |
| 129 |
|
| 130 |
# change links to Main/FAQ to go to FAQ.html |
| 131 |
$text =~ s!$ARDUINO/Main/FAQ!FAQ.html!xg; |
| 132 |
|
| 133 |
# change links to the reference HomePage to go to index.html |
| 134 |
$text =~ s!HomePage.html!index.html!xg; |
| 135 |
|
| 136 |
# change links to the reference edit site to go to the public site |
| 137 |
$text =~ s!$ARDUINO!$PUBLIC!xg; |
| 138 |
|
| 139 |
# change links to the root directory to go to the Arduino home page |
| 140 |
$text =~ s!href="/"!href="http://www.arduino.cc"/!xg; |
| 141 |
|
| 142 |
return $text; |
| 143 |
} |
| 144 |
|
| 145 |
######################################################################## |
| 146 |
# create_linked_pages($text, $link_pattern, $page_name) |
| 147 |
# |
| 148 |
# Scan the given text for links matching the $link_pattern and |
| 149 |
# create local files for the linked pages. |
| 150 |
# |
| 151 |
# The link_pattern is a regexp with one parenthesized subexpression - |
| 152 |
# the text matching the subexpression will replace the |
| 153 |
# special pattern '%%' in the $page_name to generate the name of |
| 154 |
# the local file. |
| 155 |
# |
| 156 |
sub create_linked_pages { |
| 157 |
my $text = shift; |
| 158 |
my $link_pattern = shift; |
| 159 |
my $new_name = shift; |
| 160 |
|
| 161 |
while ($text =~ m!$link_pattern!g) { |
| 162 |
my ($url, $name) = ($&, $1); |
| 163 |
(my $page = $new_name) =~ s!%%!$name!; |
| 164 |
next if $name =~ /\?/ || $downloaded{$url}; |
| 165 |
create_page($page, $url); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
#---------------------------- end of code ------------------------------ |