Loading ...
Sorry, an error occurred while loading the content.

Fix Links on Web Pages

Expand Messages
  • Andy Young
    Hi, I ve got the third clip finished. Thanks to some help :) I ll be updating the other clips with a fix that bypasses anchor tags that don t contain a file
    Message 1 of 1 , Jul 24, 2001
    • 0 Attachment
      Hi,
      I've got the third clip finished. Thanks to some help :) I'll be
      updating the other clips with a fix that bypasses anchor tags that
      don't contain a file name, ie href="#top". I think these web site
      management clips fill a need in NoteTab and make it a better product
      for web development. If they test out alright for the clippers and
      incorporate suggestions. My intention is to try to get them added to
      the NoteTab website. Stephen can I add your sitemap clip and make it
      a bundle of four clips for website management? Thanks in advance.
      Andy


      ---------< start clip >---------
      H="LinkFix"
      ;Andy Young <ajyoung@...>
      ;07/24/2001, 12:56:48 PM
      ;
      ;----------------------[begin long line]---------------------------
      ;A special thanks to Stephen Riddle and Ramon Silvera for thier help
      with this clip.
      ;-----------------------[end long line]----------------------------
      ;
      ;
      ;----------------------[begin long line]---------------------------
      ;The info boxes and verify delete are in this clip as a safety
      precaution. In case there is an unexpected error in the clip.
      ;-----------------------[end long line]----------------------------
      ;
      ;
      ;----------------------[begin long line]---------------------------
      ^!Continue This Clip searches web pages (*.*htm*;*.asp;*.xml;*.php)
      in a specified directory and/or subdirectories for invalid local
      href links. A search is conducted to find the correct link and
      replace it for the invalid link. If a link cannot be found the user
      is prompted to suggest an alternative page name. If no link is
      provide then the link is added to a text file named linkfix.txt in
      the web site's root directory for latter
      reference.^%NL%^%NL%Note:^%NL%1. Anchor links within comments
      <-- --> are included.^%NL%2. Links containing parentheses will
      generate false invalid links.^%NL%3. This script will generate
      errors if there are duplicate file names in different
      directories.^%NL%4. This clip ignores anchor links which do not
      include a file name.
      ;-----------------------[end long line]----------------------------
      ;
      ^!ClearVariables
      ^!SetScreenUpdate Off
      ^!SetPasteIndent Off
      ^!SetWordWrap Off

      ;Gather user variables
      ;
      ;----------------------[begin long line]---------------------------
      ^!Set %directory%=^?[(T=D)Please give full path to directory you
      wish to search (end with backslash)=D:\My Documents\WWWDEV\]
      ;-----------------------[end long line]----------------------------
      ;
      ^!Set %docname%=^?[Name for document to store invalid links?
      =linkfix.txt]
      ;
      ;----------------------[begin long line]---------------------------
      ^!Set %include%=^?[Do you wish to include
      subdirectories?==_Yes^=+|No^=^%EMPTY%]
      ;-----------------------[end long line]----------------------------
      ;
      ;
      ;----------------------[begin long line]---------------------------
      ^!Set %rootdir%=^?[(T=D)What is the root directory of the website
      (end with backslash)=D:\My Documents\WWWDEV\]
      ;-----------------------[end long line]----------------------------
      ;
      ^!Set %kindof%=*.*htm*;*.asp;*.xml;*.php

      ;Set up arrays for search directory(ies)
      ^!SetScreenUpdate On
      ^!StatusShow Gathering Directory Information ...
      ^!SetScreenUpdate Off
      ^!SetListDelimiter ^P
      ^!SetArray
      %items%=^$GetFiles("^%include%^%directory%";"^%kindof%";*;name)$
      ;sort array so that files in root directory are first
      ^!Set %counter%=1
      :loopsort
      ;check if each member of the array has more "\" than base directory
      ;
      ;----------------------[begin long line]---------------------------
      ^!If ^$StrCount("\";"^%directory%";False;False)$ <>
      ^$StrCount("\";"^%items^%counter%%";False;False)$ loopskip
      ;-----------------------[end long line]----------------------------
      ;
      ^!Append %dirtemp%=^%items^%counter%%|
      ^!GoTo loopcounter
      :loopskip
      ^!Append %subtemp%=^%items^%counter%%|
      :loopcounter
      ^!Inc %counter%
      ^!If ^%counter% > ^%items0% SKIP
      ^!GoTo loopsort
      :endloopsort
      ^!Set %itemtemp%=^%dirtemp%^%subtemp%
      ^!Set %itemtemp%=^$StrDeleteRight("^%itemtemp%";1)$
      ^!SetListDelimiter |
      ^!SetArray %items%=^%itemtemp%
      ;Set up array for entire web site to search for broken links
      ^!SetArray %rootfiles%=^$GetFiles("+^%rootdir%";"^%kindof%";*;name)$

      ;Add title line to results file
      ;
      ;----------------------[begin long line]---------------------------
      ^!Append %file%=The following files had missing links which this
      clip could not fix.^%NL%
      ;-----------------------[end long line]----------------------------
      ;

      ;Determine if directory has changed as each file is searched.
      ;And add new directory names to results file
      ^!Set %counter%=1
      :itemloop
      ^!If ^%counter% <> 1 newdir
      ^!Append %file%=^%NL%^%directory%
      ^!GoTo samedir
      :newdir
      ^!If "^$GetPath(^%items^%counter%%)$" = "^%oldpath%" samedir
      ^!Append %file%=^%NL%^$GetPath(^%items^%counter%%)$
      ^!Set %filecheck%=0

      ;Begin searching current file for links
      :samedir
      ^!SetScreenUpdate On
      ^!StatusShow Searching ^%items^%counter%% ...
      ^!SetScreenUpdate Off
      ^!Open ^%items^%counter%%
      ^!FocusDoc
      ^!Jump DOC_START
      :linkloop
      ^!ClearVariable %url%
      ^!ClearVariable %verlink%
      ^!ClearVariable %lbsiz%
      ^!ClearVariable %lbpos%
      ^!ClearVariable %anchor%
      ^!Find "<a href="[^#]" SRI
      ^!IfError nextfile
      ^!MoveCursor +2
      ^!Select URL
      ;check to see if link is a local link
      ^!Find "(http)|(www)|(@)" IHRS
      ^!IfError validlink
      ^!Goto skiplink

      ;Convert link to usable format and check to see if link is accurate
      :validlink
      ^!Select URL
      ^!Set %url%=^$GetSelection$
      ^!Set %lbsiz%=^$StrSize("^%url%")$
      ^!Set %lbpos%=^$StrPosRight("#";"^%url%";False)$
      ^!If ^%lbpos% = 0 skip_2
      ^!Set %url%=^$StrDeleteRight("^%url%";^$Calc(^%lbsiz%-^%lbpos%+1)$)$
      ^!Set %anchor%=^$StrDeleteLeft("^%url%";^$Calc(^%lbpos%-1)$)$
      ^!Set
      %verlink%=^$GetExpandedName(^%url%;^$GetPath(^%items^%counter%%)$)$
      ^!Set
      %verlink%=^$StrReplace("file:///D|/";"D:\";"^%verlink%";False;False)
      $
      ^!Set %verlink%=^$StrReplace("/";"\";"^%verlink%";False;False)$
      ^!Set
      %verlink%=^$StrReplace("%20";"^%SPACE%";"^%verlink%";False;False)$
      ^!IfFileExist "^%verlink%" skiplink

      ;^!SetDeBug On

      ;Convert link to usable format and search for filename in other
      directories
      ^!Set %tempfile%=^%url%
      ^!Set %temppos%=^$StrPosRight("/";"^%tempfile%";False)$
      ^!Set %tempfile%=^$StrDeleteLeft("^%tempfile%";^%temppos%)$
      :tryagain
      ^!ClearVariable %yn%
      ^!Set %tmpctr%=1
      :srchloop
      ^!If ^$StrPos("^%tempfile%";"^%rootfiles^%tmpctr%%";False)$ > 0
      found
      ^!Inc %tmpctr%
      ^!If ^%tmpctr% > ^%rootfiles0% notfound
      ^!GoTo srchloop

      ;Results of search, if file is found or not found.
      :found
      ;
      ;----------------------[begin long line]---------------------------
      ^!Info The invalid link "^%url%" in file "^%items^%counter%%" was
      found to be "^%tempfile%" at "^%rootfiles^%tmpctr%%".
      ;-----------------------[end long line]----------------------------
      ;
      ;
      ;----------------------[begin long line]---------------------------
      ^!Set
      %newurl%=^$GetRelativeName(^%rootfiles^%tmpctr%%;^$GetPath(^%items^%
      counter%%)$)$
      ;-----------------------[end long line]----------------------------
      ;
      ^!Set %newurl%=^$StrReplace("\";"/";"^%newurl%";False;False)$
      ^!Set %newurl%=^$FileToUrl("^%newurl%")$
      ^!Set %newurl%=^$StrReplace("D:\My ";"";"^%newurl%";False;False)$
      ^!If ^%lbpos% = 0 skip
      ;
      ;----------------------[begin long line]---------------------------
      ^!Set %yn%=^?{(T=C)Do you wish to replace ^%url% with
      ^%newurl%^%anchor%==_yes|no}
      ;-----------------------[end long line]----------------------------
      ;
      ^!Set %yn%=^?{(T=C)Do you wish to replace ^%url% with
      ^%newurl%==_yes|no}
      ^!If ^%yn% = yes replaceurl
      ^!Goto recordurl
      :notfound
      ;
      ;----------------------[begin long line]---------------------------
      ^!Info The file "^%tempfile%" for the ^%url% was not found in entire
      website.
      ;-----------------------[end long line]----------------------------
      ;
      ;
      ;----------------------[begin long line]---------------------------
      ^!Set %yn%=^?{(T=C)Do you wish to suggest another filename for
      ^%tempfile%==_yes|no}
      ;-----------------------[end long line]----------------------------
      ;
      ^!If ^%yn% = no recordurl
      ^!Set %tempfile%=^?{Give new name for ^%tempfile%=^%tempfile%}
      ^!GoTo tryagain

      ;paste new url in document
      :replaceurl
      ^!If ^%lbpos% = 0 skip
      ^!SetClipboard ^%newurl%^%anchor%
      ^!SetClipboard ^%newurl%
      ^!Select URL
      ^!IfError ^!Continue
      ^!PasteSelect
      ^!GoTo skiplink

      ;Add invalid link with file name to results file
      :recordurl
      ^!If "^%filecheck%" = "1" Skip_2
      ^!Append %file%=^%NL%^%TAB%^$GetFileName(^%items^%counter%%)$
      ^!Set %filecheck%=1
      ^!Append %file%=^%NL%^%TAB%^%TAB%^%url%

      :skiplink
      ;^!SetDeBug Off
      ^!Jump Select_End
      ^!Goto linkloop

      ;All links in file checked save file and repeat again on remaining
      files
      :nextfile
      ^!Close "^%items^%counter%%" SAVE
      ^!IfError End
      ^!Set %oldpath%=^$GetPath(^%items^%counter%%)$
      ^!Inc %counter%
      ;^!Continue to next file?
      ^!If ^%counter% > ^%items0% SKIP
      ^!GoTo itemloop

      ;Create results files
      ^!Toolbar New Document
      ^!InsertHtml ^%file%
      ^!Save:As "^%rootdir%^%docname%"

      :end
      ^!SetScreenUpdate On
      ^!SetPasteIndent On
      ^!SetWordWrap On
      ^!ClearVariables
      ---------< end clip >-----------
    Your message has been successfully submitted and would be delivered to recipients shortly.