home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / scripts / ckermit / ksitemap.~1~ < prev    next >
Text File  |  2010-12-07  |  11KB  |  264 lines

  1. #!/usr/local/bin/kermit +
  2. #
  3. # ksitemap v0.90 Tue Dec  7 17:21:33 2010
  4. #
  5. .encoding = ISO-8859-1  # Change to UTF-8 or other valid encoding if desired
  6. .testingdest = ~/tmp/   # Result directory for testing - change if necessary
  7.  
  8. # Builds a sitemap.xml file for a website, with Google image extensions.
  9. # Requires: C-Kermit 9.0 Alpha.03 or later.
  10. #
  11. # Documentation: http://www.kermit.edu/ksitemap.html
  12. #
  13. # Optional command-line argument: path of filelist file.  If the argument is
  14. # given the web directory is assumed to be the same directory where the
  15. # filelist is.  If not given, a file named "filelist" in the current directory
  16. # is assumed if it exists.  For details see the documentation or read below.
  17. #
  18. # Data file summary... ON TOP:
  19. # home=value -- URL of website home directory
  20. # geo=value  -- String: location of website
  21. # lic=value  -- Filename of page containing copyright/license info
  22. #
  23. # REST OF FILE: Information for each page to be included in sitemap:
  24. # url=value  -- Filename of web page (required)
  25. # pri=value  -- Priority for indexing (0.0 to 1.0) (optional)
  26. # img=value  -- Filename of an image used in this web page (optional)
  27. # cap=value  -- Caption for this image (optional)
  28. # title=value - Title for thie image (optional)
  29. #
  30. # Any page can have between 0 and 1000 images in the sitemap.
  31. #
  32. # Author: Frank da Cruz, December 2010.
  33. #
  34. if llt \v(version) 900299 exit 1 "C-Kermit 9.0 or later required"
  35.  
  36. if def \$(DEBUG) set debug message on    # DEBUG env variable requests debugging
  37. .unix = 0                # For "if unix ..."
  38. if equ "\v(system)" "UNIX" .unix = 1
  39. .usedenv = 0
  40.  
  41. def errexit {                # Fatal error macro
  42.     echo \v(timestamp) \v(dir) sitemap.ksc:
  43.     echo Error: \%*
  44.     exit 1
  45. }
  46. if def \%1 {                # Command-line argument if any
  47.     .webdirectory := \fdirname(\%1)    # is pathname of file list file.
  48.     if def webdirectory {        # If it includes a directory part
  49.         if not directory \m(webdirectory) { # Check it
  50.             errexit NOT A DIRECTORY: \m(webdirectory)
  51.         }
  52.         cd \m(webdirectory)        # and CD to it
  53.         if fail errexit CD FAILED: \m(webdirectory)
  54.     }
  55.     .filelist := \fbasename(\%1)    # And this is the name of the file
  56.     if not def filelist .filelist = filelist
  57. } else if def \$(WEBSITEDIR) {        # Env variable WEBSITEDIR exists
  58.     .webdirectory := \$(WEBSITEDIR) 
  59.     if not directory \m(webdirectory) { # Check it
  60.     errexit "NOT A DIRECTORY: \m(webdirectory) [From $WEBSITEDIR]"
  61.     }
  62.     cd \m(webdirectory)            # and CD to it
  63.     if fail errexit "CD \m(webdirectory) [From $WEBSITEDIR]"
  64.     .filelist = filelist        # And the file-list file is filelist
  65.     .usedenv = 1
  66. } else {                # Otherwise
  67.     .webdirectory := \v(dir)        # assume the current directory
  68.     .filelist = filelist        # And default the filename to filelist
  69. }
  70. .resultdirectory := \m(webdirectory)    # Where to put sitemap.xml
  71.  
  72. if debug {                # Debugging
  73.     .resultdirectory := \m(testingdest)
  74.     echo DEBUGGING...
  75.     if usedenv echo Parameters obtained from $WEBSITEDIR environment variable:
  76.     show mac webdirectory filelist
  77.     echo Writing result to \m(testingdest)\m(filelist)
  78.     echo current directory is \v(dir)
  79. }
  80. if not exist \m(filelist) {        # Check that the file list file exists
  81.     errexit FILE LIST NOT FOUND: \m(webdirectory)\m(filelist)
  82. }
  83. # Define some macros...
  84.  
  85. define FERREXIT {            # Fatal error reading file list file
  86.     exit 1 [\flpad(\m(lineno),3,0)] \%1 [\m(line)]
  87. define FERRWARN {            # Warning about a file list line
  88.     echo [\flpad(\m(lineno),3,0)] \%1 [\m(line)]
  89. }
  90.  
  91. define FINISHIMAGE {            # Macro to write Image epilog
  92.     if inimg {                # If we were doing an image...
  93.     if def geo {            # if location defined
  94.         .\%9 := <image:geo_location>\m(geo)</image:geo_location>
  95.         fwrite /line \%o "    \%9"    # add it.
  96.     }
  97.     if def lic {            # If license URL defined
  98.         .\%9 := <image:license>\m(home)\m(lic)</image:license>
  99.         fwrite /line \%o "    \%9"    # Add it
  100.     }
  101.     fwrite /line \%o "  </image:image>" # Close image clause
  102.     .inimg = 0            # No longer doing an image
  103.     }
  104. }
  105. def FINISHFILE {            # Macro to write URL epilog
  106.     if not inurl end 0
  107.     finishimage                # Finish current image if any
  108.     fwrite /line \%o "  <priority>\m(priority)</priority>" # Add page priority
  109.     fwrite /line \%o </url>        # End of this URL
  110.     .inurl = 0                # No longer doing a URL
  111. }
  112. # Begin execution...
  113.  
  114. fopen /read \%c \m(filelist)        # Open the file-list file
  115. if fail errexit "\v(lastcommand)"    # Make sure it is open
  116.  
  117. fopen /write \%o sitemap.tmp        # Open the temporary sitemap file.
  118. if fail errexit "OPEN /WRITE FAILED"    # Check
  119.  
  120. # Write XML prolog to sitemap file...
  121. fwrite /line \%o <?xml version="1.0" encoding="\m(encoding)"?> # First line
  122. if fail errexit "WRITE FAILED"        # Check that FWRITE succeeded
  123.  
  124. # If we get here all writes should succeed - continue the XML prolog.
  125. fwrite /line \%o <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  126.  
  127. .urls = 0                # Initialize URL counter
  128. .imgs = 0                # Image counter
  129. .inurl = 0                # State flag: doing a URL
  130. .lineno = 0                # File list file line number
  131. .mjd := \fmjd(today)            # Today's date MJD format
  132. .tags = |url|pri|img|cap|title|home|geo|lic| # Valid tags (for easy checking)
  133. .home =                    # Web home directory (none yet)
  134. .globalgeo =                # Global geographic location (ditto)
  135. .globallic =                # Global license page (ditto)
  136.  
  137. while true {                # Loop to read and process file list
  138.     fread /line /trim \%c line          # Read a line and trim trailing blanks
  139.     if fail break                       # Fail = end of file = all done
  140.     increment lineno                    # Count this line
  141.     if not defined line continue        # If empty line read the next one
  142.     .line := \fltrim(\m(line))          # Trim leading blanks
  143.     if eq "\s(line[1:1])" "#" continue  # If it's a comment line keep reading
  144.     .\%9 := \fsplit(\m(line),&x,=,ALL,1) # Split line on '='
  145.     if < \%9 2 { ferrwarn "TAG WITH NO VALUE", continue }
  146.     .s1 := \&x[1]            # Tag
  147.     .s2 := \&x[2]            # Value
  148.  
  149.     if not \findex(|\m(s1)|,\m(tags)) {    # Preverify tag
  150.         ferrwarn "UNKNOWN TAG '\m(s1)'- SKIPPING"
  151.         continue
  152.     }
  153.     if equ "\m(s1)" "home" {        # Website home directory
  154.         # In Unix supply trailing slash if necessary
  155.         if unix if neq "\fright(\m(s2),1)" "/" .s2 := \m(s2)/
  156.         .home := \m(s2)            # to be used in building URLs
  157.         continue
  158.     }
  159.     if equ "\m(s1)" "geo" {        # Image geographic location
  160.         if == 0 urls {            # If geo given at the head of filelist
  161.         .globalgeo := \m(s2)     # make it the global default value
  162.         } else {
  163.             .geo := \m(s2)        # set the local value in any case
  164.         }
  165.         continue
  166.     }
  167.     if equ "\m(s1)" "lic" {        # Website license page
  168.         if == 0 urls {            # Same as for geo
  169.             .globallic := \m(s2)
  170.         } else {
  171.             .lic := \m(s2)
  172.         }
  173.         continue
  174.     }
  175.     if equ "\m(s1)" "url" {        # Wep age URL
  176.         if not def home errexit "URL BEFORE HOME DEFINED"
  177.         if inurl do finishfile        # Finish previous URL if any
  178.         .priority = 0.5            # Default page priority (0.0-1.0)
  179.         .name := \m(s2)            # Filename of this web page
  180.     if not exist \m(name) { ferrwarn "NOT EXIST \m(name)", continue }
  181.     if not readable \m(name) { ferrwarn "NOT READABLE \m(name)" }
  182.         .inurl = 1            # We are doing a URL now
  183.  
  184.     message \m(name)...        # List the name if debugging
  185.     incr urls            # Count the URL
  186.     fwrite /line \%o <url>        # Start the XML URL section
  187.         # Add URL of this file to sitemap...
  188.     if eq "\m(name)" "index-en.html" { # (Special for multilingual sites)
  189.         fwrite /line \%o "  <loc>\m(home)</loc>"
  190.         } else if eq "\m(name)" "index.html" {
  191.         fwrite /line \%o "  <loc>\m(home)</loc>"
  192.     } else {
  193.         fwrite /line \%o "  <loc>\m(home)\m(name)</loc>"
  194.     }
  195.     .s := \fcvtd(\fdate(\m(name)),3) # Modification date of file
  196.     .s := \s(s[1:4])-\s(s[5:2])-\s(s[7:2]) # Just the date is enough
  197.     fwrite /line \%o "  <lastmod>\m(s)</lastmod>" # Add to sitemap
  198.         .\%x := \fmjd(\fdate(\m(name)))    # Modification date as MJD
  199.         .\%y ::= \m(today) - \%x    # How many days ago
  200.         .c = yearly            # Default change frequency is yearly
  201.         if < \%y 8 .c =  daily         # If modified in last 7 days daily
  202.         else if < \%y 30 .c =  weekly    # or in last 30 days say weekly
  203.         else if < \%y 100 .c =  monthly    # or in last 100 days say monthly
  204.     fwrite /line \%o "  <changefreq>\m(c)</changefreq>" # Add to sitemap
  205.         continue
  206.     }
  207.     if equ "\m(s1)" "pri" {        # Page priority
  208.         if not inurl ferrexit "PRIORITY NOT IN URL"
  209.         if not float \m(s2) ferrexit "PRIORITY NOT NUMERIC"
  210.         if ( > \m(s2) 1.0 || < \m(s2) 0.0 ) ferrexit "PRIORITY OUT OF RANGE"
  211.         .priority := \m(s2)        # Save it for epilog (see finishfile)
  212.         continue
  213.     }
  214.     if equ "\m(s1)" "img" {        # Image
  215.         finishimage            # Finish previous image if any
  216.         .geo := \m(globalgeo)        # If a global one defined use it
  217.         .lic := \m(globallic)        #  wherever a local one is not given.
  218.         if not inurl ferrexit "img not in url"
  219.     if not exist \m(s2) { ferrwarn "IMG NOT EXIST: \m(s2)", continue }
  220.     if not readabl \m(s2) { ferrwarn "IMG NOT READABLE: \m(s2)", continue }
  221.         increment imgs            # Count this image
  222.         fwrite /line \%o "  <image:image>" # Start image clause
  223.         fwrite /line \%o "    <image:loc>\m(home)\m(s2)</image:loc>" # Put URL
  224.         .inimg = 1            # We are doing an image now
  225.         continue
  226.     }
  227.     if equ "\m(s1)" "cap" {        # Image caption
  228.         if not inimg ferrexit "CAP WITH NO IMG"
  229.         if def s2 {            # If the caption is not empty add it
  230.             fwrite /line \%o "    <image:caption>\m(s2)</image:caption>"
  231.         }
  232.         continue
  233.     }
  234.     if equ "\m(s1)" "title" {        # Image title
  235.         if not inimg ferrexit "TITLE WITH NO IMG"
  236.         if def s2 {            # If the title is not empty add it
  237.             fwrite /line \%o "    <image:title>\m(s2)</image:title>"
  238.         }
  239.     }
  240. }
  241. do finishfile                # End of file list - finish last URL
  242. fwrite /line \%o </urlset>        # Finish the sitemap
  243. fclose \%o                # Close the temporary sitemap file
  244. if exist sitemap.xml {            # Rotate previous ones
  245.     if exist sitemap.ayer copy /preserve sitemap.ayer sitemap.ante
  246.     if fail message "FAILURE TO ROTATE OLD SITEMAP[1]"
  247.     copy /preserve sitemap.xml sitemap.ayer
  248.     if fail message "FAILURE TO ROTATE OLD SITEMAP[2]"
  249. }
  250. rename sitemap.tmp \m(resultdirectory)sitemap.xml # Install the new sitemap
  251. if fail errexit "FAILURE TO INSTALL NEW SITEMAP"
  252. if unix {                # Unix...
  253.     chmod 644 sitemap.xml        # Make it world readable
  254.     if fail errexit "CHMOD FAILURE"
  255. }
  256. # When run in a cron job this message arrives in email
  257. exit 0 "[\v(timestamp)] sitemap.ksc: OK - URLs: \m(urls); IMGs: \m(imgs)"
  258.  
  259. ; Local Variables:
  260. ; comment-column:40
  261. ; comment-start:"# "
  262. ; End:
  263.