Page 1 of 5

Web Programming

Posted: Sun 19 Dec 2010, 21:44
by technosaurus
I intentionally did not specify html programming in the topic, since web programming can take several forms.

Please post your web programming tip, tricks or questions here.

Posted: Sun 19 Dec 2010, 21:45
by technosaurus
Here is a script that will generate an html index of directories and files.

Code: Select all

#!/bin/sh
# $1 is a directory to index as html
cd $1;
for x in *; do [ -d "$x" ] && D=$D"<li><a href=\"$x/\">$x/</a></li>" || F=$F"<li><a href=\"$x\">$x</a></li>"; done
echo "<html><head><title>index of $1</title></head><body><p><b>index of $1</b></p><p>directories:</p><ul><li><a href=\"../\">[parent directory]</a></li>$D</ul><p>files:</p><ul>$F</ul></body></html>" >$1/index.html
there is a C version of this script here:
http://www.mathopd.org/dist/dir_cgi.c.txt

Posted: Sun 19 Dec 2010, 22:09
by trapster
Not working well with spaces in file names.

Posted: Sun 19 Dec 2010, 23:46
by big_bass
Hey trapster
Not working well with spaces in file names.


this will auto fix /heal all folders and files with spaces or symbols and convert them to underscore "_"

just place it in the folder with the bad names and run it


all is automatic

Joe

Code: Select all

#!/bin/bash

# Joe Arose version 1.05  
# sana_auto  =  auto find and repair 
# sana =  sanitize  these =;:"`<>,!@#$?%^*&(){}[]
# sana =  sana sana colita de rana   "for the spanish translation" 
# drag N drop rename files that have been badly named 
# to a corrected format with underscores 
# many bad characters get deleted  
# programs will not function correctly with spaces placed in the name 
# note that permissions are preseved !


rm -f /tmp/sanitized
rm -f /tmp/borked_name
rm -f /tmp/fix_borked_names.txt


#this is faster fix directories first later it will fix the files 


find $pwd -type d | tr " " "?" | tr  '=;"`<>,!@#$?%^*&(){}[]' '?'| grep '?' >>/tmp/fix_borked_names.txt

#so you have a list of changed files that have ???
cp /tmp/fix_borked_names.txt /tmp/fix_borked_names_dir$$


##special function to clean everything 

for pkg in `cat /tmp/fix_borked_names.txt` ;do
echo "$pkg" | tr -d '=;`"<>,?!@#$%^*&(){}[]' |tr -s ' ' '_*' >/tmp/sanitized
echo "$pkg" | tr " " "?" | tr  '=;"`<>,!@#$?%^*&(){}[]' '?' >/tmp/borked_name


rename_bork=`cat /tmp/borked_name`
echo  "$rename_bork"


for_cleaned=`cat /tmp/sanitized`
echo "$for_cleaned"

mv $rename_bork $for_cleaned 



 Xdialog --title "working on directories" \
        	--infobox "\nConversion to $for_cleaned has finished.\n" 0 0 1000

done 


 Xdialog --title "Complete" \
        	--infobox "\n Auto conversion folders  finished.\n" 0 0 3000

#-----------------------------
#  fix all the files now
#----------------------------

rm -f /tmp/sanitized
rm -f /tmp/borked_name
rm -f /tmp/fix_borked_names.txt


find $pwd -type f | tr " " "?" | tr  '=;"`<>,!@#$?%^*&(){}[]' '?'| grep '?' >>/tmp/fix_borked_names.txt


#so you have a list of changed files that have ???
cp /tmp/fix_borked_names.txt /tmp/fix_borked_names_files$$


##special function to clean everything 


for pkg in `cat /tmp/fix_borked_names.txt` ;do
echo "$pkg" | tr -d '=;`"<>,?!@#$%^*&(){}[]' |tr -s ' ' '_*' >/tmp/sanitized
echo "$pkg" | tr " " "?" | tr  '=;"`<>,!@#$?%^*&(){}[]' '?' >/tmp/borked_name





rename_bork=`cat /tmp/borked_name`
echo  "$rename_bork"


for_cleaned=`cat /tmp/sanitized`
echo "$for_cleaned"

mv $rename_bork $for_cleaned 



 Xdialog --title "working on files" \
        	--infobox "\nConversion to $for_cleaned has finished.\n" 0 0 1000

done 


 Xdialog --title "Complete" \
        	--infobox "\n Auto conversion files  finished.\n" 0 0 3000

Posted: Mon 20 Dec 2010, 00:04
by trapster
big_bass -
Thanks for the offer and I will save this for future use but I have 6,000 songs + movies already running on my server and I don't want to mess with the filenames.

I do use "tree" to create an html file index.
ie:

Code: Select all

tree -a -H /mnt/home/music > /mnt/home/music/index.html
Works a treat.

Posted: Mon 20 Dec 2010, 00:56
by big_bass
Hey technosaurus ,trapster

well here is a real simple basic index maker and easy to edit
for anyone to hack at


Code: Select all

#!/bin/sh

#code from Joe Arose big_bass built for special use
# generate a custom index
# drag N drop a folder on the script

#with the end goal of easily making the correct formatting
#simple and clean
#as just the URL's then generate a new index

#/root/new_index_list  #this is the index generated




#---------------index maker--------------------------------
echo '
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of </title>
 </head>
 <body>
<h1>Index of  new_index_list</h1>
<pre><img src="/usr/local/lib/X11/pixmaps/archive48.png" alt="Icon "> <a href="?C=N;O=D">Name</a>' >/root/new_index_list



for i in `find "$@"/*`
do echo "<"a href="$i"">"$i"<"/a">" >>/root/new_index_list
done

echo '<hr></pre>
<address>Apache Server at distro.ibiblio.org Port 80</address>
</body></html>' >>/root/new_index_list

#you can edit this if you have seamonkey 
#gtkmoz /root/new_index_list 
/usr/lib/firefox/firefox /root/new_index_list 


Posted: Mon 20 Dec 2010, 01:05
by abushcrafter
http://www.catb.org/~esr/imgsizer/
This tool auto-generates or corrects WIDTH and HEIGHT parameters into HTML IMG tags, making page loading faster.

Posted: Mon 20 Dec 2010, 01:37
by technosaurus
Use this to remove spaces:

Code: Select all

for x in *; do X="`echo $x|tr ' ' '_'`";[ "$x" != "$X" ] && mv "$x" "$X"; done
I used * to fix the initial version too - so it should allow spaces when I repost it

Posted: Mon 20 Dec 2010, 16:21
by big_bass
Hey technosaurus


I liked the visual output of the generated code you did nice job 8)
It however was difficult to read the source code
I combined my code with yours to have both
the source and the output pretty :D

thanks for starting this thread
and posting your code

Joe

*this takes more lines of course but the byte count is still very low
EDIT filter for files so folder isnt made for directories also

Code: Select all




#!/bin/sh

# drag N drop html index maker
# "$@" is a directory to index as html
# technosaurus author
# big_bass improved generated source code readability
 
cd "$@"

#---------------------
# clean up old debugging files
#---------------------
rm -f "@"/index.html
rm -f /tmp/flist2
rm -f /tmp/dlist2

#---------------------
# generate the directory and file index
#---------------------
for x in *
 do [ -d "$x" ] &&  echo "<li><a href=\"$x/\">$x/</a></li>" >> /tmp/dlist2
  ! [ -d "$x" ] && echo "<li><a href=\"$x\">$x</a></li>"  >>/tmp/flist2
 done
 
#---------------------
# Head  make the html head 
#---------------------
echo "<html><head><title>index of "$@"
</title></head><body><p><b>index of "$@"</b>
</p><p>directories:</p>
<ul><li><a href=\"../\">[parent directory]</a>">"$@"/index.html

#---------------------
# Body append the dynamic info  for the index here the (directories )
#---------------------
echo "</li>" >>"$@"/index.html
cat /tmp/dlist2>>"$@"/index.html
echo "</ul>">>"$@"/index.html

#---------------------
# Body  append the dynamic info  for the index here the (files )
#---------------------
echo "<p>files:</p><ul>" >>"$@"/index.html
cat /tmp/flist2 >>"$@"/index.html
echo "</ul>" >>"$@"/index.html



#---------------------
#Close the html header
#---------------------
echo "</body></html>" >>"$@"/index.html


# remove new debugging files
#rm -f /tmp/flist2
#rm -f /tmp/dlist2



Posted: Mon 20 Dec 2010, 18:27
by technosaurus
Normally I would do the same, but wanted the code to be as fast as possible in case it's used as a cgi script - thus only one loop, one check and one echo... but I could probably add some return characters and/or backslashes for readability without affecting the speed.

Posted: Tue 21 Dec 2010, 15:25
by sc0ttman
[quote="TechGuruLive.com"]Besides from using perl scripts, phpmyadmin and customized php pages, here is a quick way to fetch data from mysql database using shell script.

Just make sure you are currently running mysql from your box with existing database and table inside.

First, launch your favorite linux editor and create a bash script and paste the following bash codes inside and save.

Code: Select all

#!/bin/bash
# july 03 2007 – vertito
# this script retrieves data from mysql using bash

dbase=`mysql -uMYSQLUSENAME -pMYSQLPASSWORD -e

Posted: Wed 22 Dec 2010, 14:49
by big_bass
having code examples are great when they can be recycled

what is worth more than any code snippet in itself
is the educational value it has

if you can learn something from the code it is useful


In my opinion
good documentation is worth more than the original work itself
because it invites creativity and growth

Joe

Posted: Wed 22 Dec 2010, 15:04
by big_bass
Hey technosaurus


I recycled the code some more to take a plain text file as input
I dont have direct access to the folder /files on the server to dragndrop the file to build the index


I had earlier written my own indexing for my packages
I was happy with it but the rendering of the page was slow
due to the images and so I decided to go simple and clean
and re do it

*I had written a way to auto generate package sizes and the dates
but that only works when you have all the files on your box :?


if you (or any one else )see(s) something you would change / improve let me know




if you take a look at the link http://www.puppy2.org/slaxer/
here is a simple new index waiting on improvements

thanks
Joe

Posted: Wed 22 Dec 2010, 16:02
by technosaurus
... good idea for adding file size

need to change this:
F=$F"<li><a href=\"$x\">$x</a></li>"; done

to something like this:
F=$F"<li><a href=\"$x\">$x</a> `du $x` kb</li>"; done

Posted: Wed 22 Dec 2010, 18:21
by big_bass
I just made two strings
$datemodified
$size

here is only an example I dont use the echoes
and the package is "x"
just wanted to post a stand alone snippet

awk is nice to get exactly what you want from the output
and this also displays in MB or KB automatically

Code: Select all

# use any package for a test 
package="/root/qemu-0.91-launchscript-SDL-1.29-i486-3_SLXR.txz"


datemodified=`ls -lh "$package" | awk '{ print $6 }'`
size=`ls -lh "$package" | awk '{ print $5 }'` 

echo $datemodified
echo $size
Joe

Posted: Tue 29 Mar 2011, 22:57
by abushcrafter

Code: Select all

#!/bin/sh

# Version=0.0.4

# drag N drop html index maker
# "$@" is a directory to index as html
# technosaurus author
# big_bass improved generated source code readability
# abushcrafter: Improved output HTML.
# abushcrafter: Fixed delete existing "index.html".
# abushcrafter: Added size and modified date code by big_bass.
 
cd "$@"

#---------------------
# clean up old debugging files
#---------------------
rm -f "$@"/index.html
rm -f /tmp/flist2
rm -f /tmp/dlist2

#---------------------
# generate the directory and file index
#---------------------
for x in *
 do [ -d "$x" ] &&  echo "            <li><a href=\"$x/\">$x/</a></li>" >> /tmp/dlist2
  ! [ -d "$x" ] && echo "            <li><a href=\"$x\">$x</a> ,`ls -lh \"$x\" | awk '{ print $5 }'`KB, Date Modified=`ls -lh \"$x\" | awk '{ print $6 }'`</li>"  >>/tmp/flist2
 done
 
#---------------------
# Head  make the html head
#---------------------
echo "<html>
    <head>
        <title>Index Of: "$@"</title>
    </head>
    <body>
        <h1>Index Of "$@"</h1>
        <h2>Directories:</h2>
        <ul>
            <li><strong><a href=\"../\">[Parent Directory]</a></strong></li>">"$@"/index.html

#---------------------
# Body append the dynamic info  for the index here the (directories )
#---------------------
cat /tmp/dlist2>>"$@"/index.html
echo "        </ul>">>"$@"/index.html

#---------------------
# Body  append the dynamic info  for the index here the (files )
#---------------------
echo "        <h2>Files:</h2>
        <ul>" >>"$@"/index.html
cat /tmp/flist2 >>"$@"/index.html
echo "        </ul>" >>"$@"/index.html



#---------------------
#Close the html header
#---------------------
echo "    </body>
</html>" >>"$@"/index.html


# remove new debugging files
#rm -f /tmp/flist2
#rm -f /tmp/dlist2 

Posted: Wed 07 Nov 2012, 22:15
by technosaurus
My sprite generator is here:
http://murga-linux.com/puppy/viewtopic.php?t=82009

here is an example that slices the output into 24x24 icons

Code: Select all

<style>
.img{height:24;width:24;float:left;padding:0 0 0 0;display:block;background:url(sprite.png)}
.a{background-position:0-24}
.b{background-position:0-48}
.c{background-position:0-72}
</style>

<div class="img"></div>
<div class="img a"></div>
<div class="img b"></div>
<div class="img c"></div>

Posted: Wed 21 Nov 2012, 07:58
by technosaurus
How to localize web pages.

include this in you're page

Code: Select all

<html><head><title>Localized page</title>
<meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body>
<div id="string1">  This is the default text of string 1.  </div>
<div id="string2">  This is the default text of string 2.  </div>
</body></html>
<script>
function UrlExists(url){	var http=new XMLHttpRequest()
	try{http.open('HEAD', url, false);http.send()return http.status!=404
	}catch(e){return false}
}

function setLang(data){		var term
	for (var i=0;i<data.items.length;i++){
		term=document.getElementById(data.items[i].id)
		if (term) term.innerHTML=data.items[i].value
	}
}

var lang=navigator.userLanguage || navigator.language.toLowerCase()
if(UrlExists(lang+".js")){	var script=document.createElement("script")
	script.src=lang+".js?callback=setLang";
	document.body.appendChild(script)
}

</script>
then for each supported language, set up a file such as en-us.js

Code: Select all

setLang({"items":[
{"id" : "string1","value" : " Localized text of string1. "},
 {"id" : "string2", "value" : " Localized text of string2. "}
]});
This file goes in the same directory as your page and can be shared by all pages in that directory (with modifications it could do multiple directories or sites or only one file, but I didn't want to complicate it)

Posted: Wed 21 Nov 2012, 20:21
by technosaurus
here is the start of puppy's /usr/share/doc/index.html:

I made 2 sections so far: the intro and legal, to do the translation for es-es, just copy en-us.js to es-es.js and modify each of the "value" values ("id" and its value is not translated)

if you have set your language and it still isn't working, find this line:
var lang=navigator.userLanguage || navigator.language.toLowerCase();
and after it add
alert(lang);
you will get a message telling you what your language is, the language file should be the contents of that message (should be lower case) with a .js extension

Web Programming

Posted: Wed 21 Nov 2012, 21:14
by L18L
technosaurus wrote:...
if you have set your language and it still isn't working, find this line:
var lang=navigator.userLanguage || navigator.language.toLowerCase();
and after it add
alert(lang);
you will get a message telling you what your language is, the language file should be the contents of that message (should be lower case) with a .js extension
In seamonkey it is the language of the languagepack.
If not installed it is en-us of course, no dependency of locale setting.
de-de.js wrote:setLang(
{"id":"intro","value":"<br>Hi, ich bin Barry Kauler, der original developer of the Puppy Operating System,................
function UrlExists(url){ var http=new XMLHttpRequest()
needs a web server, ?