|
| 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 | +my $verbose = 1; |
| 13 | +my $CURL_OPTIONS = '--silent --show-error'; |
| 14 | + |
| 15 | +my $ARDUINO = 'http://www.arduino.cc/en'; # base url for arduino site |
| 16 | + |
| 17 | +my %downloaded = (); # keep track of the pages we download |
| 18 | + |
| 19 | +my $guide = create_page('Guide_index.html', "$ARDUINO/Guide/HomePage"); |
| 20 | + |
| 21 | +my $faq = create_page('FAQ.html', "$ARDUINO/Main/FAQ"); |
| 22 | +my $env = create_page('environment.html', "$ARDUINO/Main/Environment"); |
| 23 | +my $css = create_page('arduino.css', "$ARDUINO/pub/skins/arduino/arduino.css"); |
| 24 | +my $eeprom = create_page('EEPROM.html', "$ARDUINO/Reference/EEPROM"); |
| 25 | +my $stepper = create_page('Stepper.html', "$ARDUINO/Reference/Stepper"); |
| 26 | +my $softser = create_page('SoftwareSerial.html', "$ARDUINO/Reference/SoftwareSerial"); |
| 27 | + |
| 28 | +create_linked_pages($guide, qr!$ARDUINO/Guide/(\w+)!, 'Guide_%%.html'); |
| 29 | +create_linked_pages($softser, qr!$ARDUINO/Reference/(SoftwareSerial\w+)!, '%%.html'); |
| 30 | +create_linked_pages($eeprom, qr!$ARDUINO/Reference/(EEPROM\w+)!, '%%.html'); |
| 31 | +create_linked_pages($stepper, qr!$ARDUINO/Reference/(Stepper\w+)!, '%%.html'); |
| 32 | + |
| 33 | +my $index = create_page('index.html', "$ARDUINO/Reference/HomePage"); |
| 34 | + |
| 35 | +create_linked_pages($index, qr!$ARDUINO/Serial/(\w+)!, 'Serial_%%.html'); |
| 36 | +create_linked_pages($index, qr!$ARDUINO/Reference/(\w+)!, '%%.html'); |
| 37 | + |
| 38 | +my $ext = create_page('Extended.html', "$ARDUINO/Reference/Extended"); |
| 39 | + |
| 40 | +create_linked_pages($ext, qr!$ARDUINO/Reference/(\w+)!, '%%.html'); |
| 41 | + |
| 42 | +exit 0; |
| 43 | + |
| 44 | +#------------------------- end of main code ---------------------------- |
| 45 | + |
| 46 | +######################################################################## |
| 47 | +# $original_text = create_page($filename, $url) |
| 48 | +# |
| 49 | +# Download the web page at the given URL, change links to point to |
| 50 | +# the offline pages, and save it locally under the given filename. |
| 51 | +# The original (unmodified) text of the downloaded page is returned. |
| 52 | +# |
| 53 | +sub create_page { |
| 54 | + my $page = shift; |
| 55 | + my $url = shift; |
| 56 | + |
| 57 | + print "$page\n" if $verbose; |
| 58 | + my $original_text = `curl $CURL_OPTIONS $url`; |
| 59 | + die "** Unable to download $url **\n" if $? or ! $original_text; |
| 60 | + $downloaded{$url} = $page; # remember that we downloaded this page |
| 61 | + |
| 62 | + my $localized_text = localize_page($original_text); |
| 63 | + open(my $PAGE, "> $page") |
| 64 | + or die "** Unable to open $page for writing. **\n"; |
| 65 | + print $PAGE $localized_text; |
| 66 | + close $PAGE; |
| 67 | + |
| 68 | + return $original_text; |
| 69 | +} |
| 70 | + |
| 71 | +######################################################################## |
| 72 | +# $localized_text = localize_page($text) |
| 73 | +# |
| 74 | +# Rewrite links in the given text to point to the offline pages. |
| 75 | +# |
| 76 | +sub localize_page { |
| 77 | + my $text = shift; |
| 78 | + |
| 79 | + # replace links to unknown pages with links to '#' |
| 80 | + $text =~ s!$ARDUINO/Reference/[^?"']*\?[^'"]*!#!xg; |
| 81 | + |
| 82 | + # replace links to remote guide with links to local guide |
| 83 | + $text =~ s!$ARDUINO/Guide/([^']+)!Guide_$1.html!xg; |
| 84 | + |
| 85 | + # replace links to remote reference with links to local reference |
| 86 | + $text =~ s!$ARDUINO/Reference/([^']*)!$1.html!xg; |
| 87 | + |
| 88 | + # replace links to remove serial reference with links to local serial reference |
| 89 | + $text =~ s!$ARDUINO/Serial/([^']*)!Serial_$1.html!xg; |
| 90 | + |
| 91 | + # direct pages to the local style file |
| 92 | + $text =~ s!$ARDUINO/pub/skins/arduino/arduino.css!arduino.css!xg; |
| 93 | + |
| 94 | + # change links to Main/FAQ to go to FAQ.html |
| 95 | + $text =~ s!$ARDUINO/Main/FAQ!FAQ.html!xg; |
| 96 | + |
| 97 | + # change links to the reference HomePage to go to index.html |
| 98 | + $text =~ s!HomePage.html!index.html!xg; |
| 99 | + |
| 100 | + # change links to the root directory to go to the Arduino home page |
| 101 | + $text =~ s!href="/"!href="http://www.arduino.cc"/!xg; |
| 102 | + |
| 103 | + return $text; |
| 104 | +} |
| 105 | + |
| 106 | +######################################################################## |
| 107 | +# create_linked_pages($text, $link_pattern, $page_name) |
| 108 | +# |
| 109 | +# Scan the given text for links matching the $link_pattern and |
| 110 | +# create local files for the linked pages. |
| 111 | +# |
| 112 | +# The link_pattern is a regexp with one parenthesized subexpression - |
| 113 | +# the text matching the subexpression will replace the |
| 114 | +# special pattern '%%' in the $page_name to generate the name of |
| 115 | +# the local file. |
| 116 | +# |
| 117 | +sub create_linked_pages { |
| 118 | + my $text = shift; |
| 119 | + my $link_pattern = shift; |
| 120 | + my $new_name = shift; |
| 121 | + |
| 122 | + while ($text =~ m!$link_pattern!g) { |
| 123 | + my ($url, $name) = ($&, $1); |
| 124 | + (my $page = $new_name) =~ s!%%!$name!; |
| 125 | + next if $name =~ /\?/ || $downloaded{$url}; |
| 126 | + create_page($page, $url); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +#---------------------------- end of code ------------------------------ |
0 commit comments