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# 9use strict; 10use warnings; 11 12print "username: "; my $user = <STDIN>; chomp($user); 13print "password: "; my $pass = <STDIN>; chomp($pass); 14 15my $verbose = 1; 16my $CURL_OPTIONS = "--silent --show-error -u $user:$pass"; 17 18my $ARDUINO = 'http://edit.arduino.cc/en_ref'; # base url for reference site 19my $PUBLIC = 'http://www.arduino.cc/en'; # base url for public site 20 21my %downloaded = (); # keep track of the pages we download 22 23my $guide = create_page('Guide_index.html', "$ARDUINO/Guide/HomePage"); 24 25my $faq = create_page('FAQ.html', "$ARDUINO/Main/FAQ"); 26my $env = create_page('environment.html', "$ARDUINO/Main/Environment"); 27my $css = create_page('arduinoUno.css', "$ARDUINO/pub/skins/arduinoUno/arduinoUno.css"); 28my $css2 = create_page('arduinoWide.css', "$ARDUINO/pub/skins/arduinoWide/arduinoWide.css"); 29my $css3 = create_page('arduinoWideRender.css', "$ARDUINO/pub/skins/arduinoWideRender/arduinoWideRender.css"); 30my $eeprom = create_page('EEPROM.html', "$ARDUINO/Reference/EEPROM"); 31my $stepper = create_page('Stepper.html', "$ARDUINO/Reference/Stepper"); 32my $softser = create_page('SoftwareSerial.html', "$ARDUINO/Reference/SoftwareSerial"); 33my $wire = create_page('Wire.html', "$ARDUINO/Reference/Wire"); 34my $sd = create_page('SD.html', "$ARDUINO/Reference/SD"); 35my $servo = create_page('Servo.html', "$ARDUINO/Reference/Servo"); 36my $spi = create_page('SPI.html', "$ARDUINO/Reference/SPI"); 37my $mousekeyboard = create_page('MouseKeyboard.html', "$ARDUINO/Reference/MouseKeyboard"); 38my $lcd = create_page('LiquidCrystal.html', "$ARDUINO/Reference/LiquidCrystal"); 39my $ethernet = create_page('Ethernet.html', "$ARDUINO/Reference/Ethernet"); 40my $serial = create_page('Serial.html', "$ARDUINO/Reference/Serial"); 41my $stream = create_page('Stream.html', "$ARDUINO/Reference/Stream"); 42my $string = create_page('StringObject.html', "$ARDUINO/Reference/StringObject"); 43 44create_linked_pages($guide, qr!$ARDUINO/Guide/(\w+)!, 'Guide_%%.html'); 45create_linked_pages($softser, qr!$ARDUINO/Reference/(SoftwareSerial\w+)!, '%%.html'); 46create_linked_pages($eeprom, qr!$ARDUINO/Reference/(EEPROM\w+)!, '%%.html'); 47create_linked_pages($stepper, qr!$ARDUINO/Reference/(Stepper\w+)!, '%%.html'); 48create_linked_pages($wire, qr!$ARDUINO/Reference/(Wire\w+)!, '%%.html'); 49create_linked_pages($servo, qr!$ARDUINO/Reference/(Servo\w+)!, '%%.html'); 50create_linked_pages($sd, qr!$ARDUINO/Reference/(SD\w+)!, '%%.html'); 51create_linked_pages($sd, qr!$ARDUINO/Reference/(File\w+)!, '%%.html'); 52create_linked_pages($spi, qr!$ARDUINO/Reference/(SPI\w+)!, '%%.html'); 53create_linked_pages($mousekeyboard, qr!$ARDUINO/Reference/(Mouse\w+)!, '%%.html'); 54create_linked_pages($mousekeyboard, qr!$ARDUINO/Reference/(Keyboard\w+)!, '%%.html'); 55create_linked_pages($lcd, qr!$ARDUINO/Reference/(LiquidCrystal\w+)!, '%%.html'); 56create_linked_pages($ethernet, qr!$ARDUINO/Reference/(Ethernet\w+)!, '%%.html'); 57create_linked_pages($ethernet, qr!$ARDUINO/Reference/(Server\w+)!, '%%.html'); 58create_linked_pages($ethernet, qr!$ARDUINO/Reference/(Client\w+)!, '%%.html'); 59create_linked_pages($serial, qr!$ARDUINO/Serial/(\w+)!, 'Serial_%%.html'); 60create_linked_pages($string, qr!$ARDUINO/Reference/(String\w+)!, '%%.html'); 61create_linked_pages($stream, qr!$ARDUINO/Reference/(Stream\w+)!, '%%.html'); 62create_linked_pages($string, qr!$ARDUINO/Reference/(String\w+)!, '%%.html'); 63 64my $index = create_page('index.html', "$ARDUINO/Reference/HomePage"); 65 66create_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 72exit 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# 83sub 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# 106sub 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# 156sub 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 ------------------------------ 170