Script Request - Save Offline Web Pages easily

Discuss and share scripts and script files...
SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Script Request - Save Offline Web Pages easily

Post by SkyFrontier »

The idea is:

since

Code: Select all

v9.00.0015 - 2010-04-15 12:48
    ! Rename Special | Aaa Aa.*: A file named "can't rename.png" would
      convert to title case as "Can'T Rename.png". Fixed. Now an
      apostrophe is not taken as word separator anymore.
a page named as

Code: Select all

Wouldn't it be Nice?
could be translated by a single click on XYplorer from this to

Code: Select all

Wouldn_tItBeNiceG
-being the last "G" a replacing for the question mark. Capitalization matters, so the lower cases preceded by spaces must be capitalized.
-blank spaces must be eliminated.
-hyphens must be replaced by underscores.

Xyplorer just have to read a previously sentence written in clipboard and copy the replacement there again, having it ready to click and replace original names inside the "Save as..." dialog.
Any help, please...?

-this is a great idea for a Firefox extension, huh? Already searched and found none. If you could point/provide one, thanks in advance...
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Script Request - Save Offline Web Pages easily

Post by Stefan »

One way me came in mind:

Framework:

Code: Select all

$A = "Would"; 
 $L = strlen($A);
 $C = 0;
 //FOR EACH CHAR IN CHARs do:
 while($C<=$L)
 {
  $Token = substr($A,$C,1);
  if ($Token==""){break;}
  msg $Token;
  incr $C;
 }

Here the working code for this challenge:

FROM:
Wouldn't it be Nice?
TO:
Wouldn_tItBeNiceG

- Capitalization chars after an space
- blank spaces must be eliminated.
- hyphens (tick ') must be replaced by underscores.
- replace question mark an "G"

Code: Select all

/*
- Capitalization chars after an space
- blank spaces must be eliminated.
- (hyphens - ) or  (tick ' )  must be replaced by underscores.
- replace question mark an "G" 
http://www.asciitable.com/
*/

$ource = "Wo uldn't it be Nice  with   many    Spaces?"; //just as an debug test
//$ource = "<clipboard>"; //from clipboard
// to "WoUldn_tItBeNiceWithManySpacesG"

   $L = strlen($ource);
   $C = 0;
   $out="";

   while($C<=$L)
   {
     $Token1 = substr($ource,$C,1);
     if ($Token==""){break;}
     $Token2 = substr($ource,$C+1,1);

     $ASC1 = asc($Token1);
     $ASC2 = asc($Token2);
     if ($ASC1 == 32)
     {
       if ($ASC2 >= 97 && <= 122)
       {
        $out=$out. chr($ASC2 - 32);
        incr $C;
        }
     }else
     {
        $out=$out. $Token1;
     }
   incr $C;
   }
   //$out = replace($out,"-","_");
   $out = replace($out,"'","_");
   $out = replace($out,"?","G");
   //$out = replace($out," ","");
   msg $out;  //just as an debug test
   //copytext $out; //to clipboard

HTH? :lol:


.
Last edited by Stefan on 27 Jul 2010 14:26, edited 1 time in total.

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Save Offline Web Pages easily

Post by SkyFrontier »

Final code (if you allow me... lol):

Code: Select all

$ource = "<clipboard>";

   $L = strlen($ource);
   $C = 0;
   $out="";

   while($C<=$L)
   {
     $Token1 = substr($ource,$C,1);
     if ($Token==""){break;}
     $Token2 = substr($ource,$C+1,1);

     $ASC1 = asc($Token1);
     $ASC2 = asc($Token2);
     if ($ASC1 == 32)
     {
       if ($ASC2 > 97 && < 122)
       {
        $out=$out. chr($ASC2 - 32);
        incr $C;
        }
     }else
     {
        $out=$out. $Token1;
     }
   incr $C;
   }
   $out = replace($out,"-","_");
   $out = replace($out,"'","_");
   $out = replace($out,"?","G");
   //$out = replace($out," ","");
   //msg $out;  //just as an debug test
   copytext $out; //to clipboard
The last "copy $out" was replace by "copytext", which done the trick.
Thanks a lot, man!
Now my web searches are a less painful task... my arm thanks you, also! lol

Code: Select all

output: ScriptRequest_SaveOfflineWebPagesEasilyGGG
msg YEAH!!!
:mrgreen:
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Script Request - Save Offline Web Pages easily

Post by Stefan »

errata

if ($ASC2 > 97 && < 122)
have to be
if ($ASC2 >= 97 && <= 122)
or
if ($ASC2 > 96 && < 123)

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Save Offline Web Pages easily

Post by SkyFrontier »

-what's the difference in results...?
Can't even imagine how this affects the code...
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Script Request - Save Offline Web Pages easily

Post by Stefan »

SkyFrontier wrote:-what's the difference in results...?
Can't even imagine how this affects the code...
Long explanation... short "Take an look">> http://www.asciitable.com/
Watch out for 97 in "Dec" column

Or check my code with this string "This is a string to zoom"

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Save Offline Web Pages easily

Post by SkyFrontier »

As far as I can see, that's responsible for the part of the that turns "a" into "A".
-right? (Don't bother answering if it deserves a negative... spares me some embarrassment... :lol: )
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Save Offline Web Pages easily

Post by SkyFrontier »

An improved "translation dictionary" for the final script:

Code: Select all

   $out = replace($out,".","");
   $out = replace($out,"(","");
   $out = replace($out,")","");
   $out = replace($out,"{","");
   $out = replace($out,"}","");
   $out = replace($out,"[","");
   $out = replace($out,"]","");
   $out = replace($out,"-","_");
   $out = replace($out,"'","_");
   $out = replace($out,":","_");
   $out = replace($out,"=","_");
   $out = replace($out,"\","_");
   $out = replace($out,"/","_");
   $out = replace($out,">","_");
   $out = replace($out,"<","_");
   $out = replace($out,"|","_");
   $out = replace($out,"*","_");
   $out = replace($out,"^","_");
   $out = replace($out,"$","_");
   $out = replace($out,"#","_");
   $out = replace($out,"@","_");
   $out = replace($out,"~","_");
   $out = replace($out,",","_");
   $out = replace($out,"!","I");
   $out = replace($out,"?","G");
   $out = replace($out,"&","And");
   $out = replace($out,"+","Plus");
Works for me. Customize any way you like it.
For us, newbies, find this section

Code: Select all

   $out = replace($out,"-","_");
   $out = replace($out,"'","_");
   $out = replace($out,"?","G");
and completely replace it with the above.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Script Request - Save Offline Web Pages easily

Post by TheQwerty »

Reducing all of those lines into the following is one of the reason why Regular Expressions are so useful...

Code: Select all

   $out = RegexReplace($out, '[.(){}\[\]]', '');
   $out = RegexReplace($out, '[-'':=\\/<>|*^$#@~,]', '_');
   $out = replace($out,"!","I");
   $out = replace($out,"?","G");
   $out = replace($out,"&","And");
   $out = replace($out,"+","Plus");

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Save Offline Web Pages easily

Post by SkyFrontier »

TheQwerty:
Reducing all of those lines into the following is one of the reason why Regular Expressions are so useful...
-thank you! Later I will try to make your suggestions work against ( " ) pattern, which currently leads the script to halt.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

admin
Site Admin
Posts: 60526
Joined: 22 May 2004 16:48
Location: Win8.1 @100%, Win10 @100%
Contact:

Re: Script Request - Save Offline Web Pages easily

Post by admin »

I don't know what you all are trying to do here, but you might check out the new command replacelist... :wink:

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Save Offline Web Pages easily

Post by SkyFrontier »

Admin:
I don't know what you all are trying to do here, but you might check out the new command replacelist... :wink:
Came to mind lightning fast... :D
But... I'm more than happy with what we achieved on this matter. Yesterday I had to save several pages and this helped a lot, even more than I was intending when proposed the thread. Firefox itself turned into the speed limiter... :lol:
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Save Offline Web Pages easily

Post by SkyFrontier »

Nice... It works even against ( " ) and a weird character I have found on some web pages' titles, ( ῳ ) - translated as ( – ) by Firefox but not by other tools.
But can someone please tell me how (regex or simple replace) to enter a given expression (such as "XYplorer Beta Club") and have it converted as ( _ ) or (<nothing>)? Any combination I try results in nothing or script halting.
Thanks!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Save Offline Web Pages easily

Post by SkyFrontier »

Is it possible to include a capitalization check, so all converted expressions will start with capitals? Thanks!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script Request - Save Offline Web Pages easily

Post by SkyFrontier »

Severely improved version, based on Stefan's code and suggestions:

Code: Select all

  $str = "<clipboard>";

  //after Capital there MUST be a lower case
  while(1)
  {
    $test = regexreplace($str, "[A-Z][A-Z]", "XXX", 1);
    if ($test==$str){break;}
    else{
       $a = regexreplace($str, "(.*)([A-Z])([A-Z])(.*)", "$1", 1 );
       $b = regexreplace($str, "(.*)([A-Z])([A-Z])(.*)", "$2", 1 );
       $c = recase(regexreplace($str, "(.*)([A-Z])([A-Z])(.*)", "$3", 1 ), "lower");
       $d = regexreplace($str, "(.*)([A-Z])([A-Z])(.*)", "$4", 1 );
       $str = $a$b$c$d;
    }
  }

  $str = recase("$str", "title", 1);
  $str = replace($str," ","");
   $str = RegexReplace($str, '[()"{}\[\]]', '_');
   $str = RegexReplace($str, '[-'':=\\/<>ῳ|*^$#@~’,]', '_');
   $str = replace($str,"!","I");
   $str = replace($str,"?","G");
   $str = replace($str,"&","And");
   $str = replace($str,"+","Plus");
   $str = RegexReplace($str, "any excessive expression which must be deleted - ", "");
  //start a sentence with capitals
  $str = recase( substr($str, 0, 1), "upper") . substr($str, 1);

  //make capitals after "_"
  while(1)
  {
    $test = regexreplace($str, "_[a-z]", "XXX", 1);
    if ($test==$str){break;}
    else{
       $a = regexreplace($str, "(.*?)_([a-z])(.*)", "$1", 1 ) . "_";
       $b = recase(regexreplace($str, "(.*?)_([a-z])(.*)", "$2", 1 ), "upper");
       $c = regexreplace($str, "(.*?)_([a-z])(.*)", "$3", 1 );
       $str = $a$b$c;
    }
  }


  //make capitals after numbers
  while(1)
  {
    $test = regexreplace($str, "\d[a-z]", "XXX", 1);
    if ($test==$str){break;}
    else{
       $a = regexreplace($str, "(.*?\d+)([a-z])(.*)", "$1", 1 );
       $b = recase(regexreplace($str, "(.*?\d+)([a-z])(.*)", "$2", 1 ), "upper");
       $c = regexreplace($str, "(.*?\d+)([a-z])(.*)", "$3", 1 );
       $str = $a$b$c;
    }
  }

  //regEx to get rid of underscores when they are followed by any extension: File_.exe >> File.exe.
   $str = regexreplace($str, "(.+)_(\..+)", "$1$2");
   $str = replace($str,".","");

   copytext $str; //to clipboard
   status "Clipboard: '$str'!";
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Post Reply