summaryrefslogtreecommitdiffstats
path: root/kjsembed/docs/examples/htmlinfo/htmlinfo.js
blob: 6b5609ca8840bf7dfe550d21eb85443181ba54b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env kjscmd

//
// Script to report info about an HTML file.
//

function parse_attrs( src )
{
    var attrs = new Array();
    var attr_name;
    var attr_value;

    while ( src.length > 0 ) {
	// Attr name
	var am = src.match( /^\s*(\w+)\s*(=?)\s*/ );
	attr_name = am[1];
	src = src.slice( am[0].length );
    
	// If attr has a value
	if ( am[2] == '=' ) {

	    if ( src.charAt(0) == '"' ) {
                vm = src.match( /(\"[^\"]*\")\s*/ );
		attr_value = vm[1];
		src = src.slice( vm[0].length );
	    }
	    else {
		vm = src.match( /(\S+)\s*/ );
		attr_value = vm[1];
		src = src.slice( vm[0].length );
	    }

	}
	else {
	    attr_value = null;
	}
	src.replace( /^\s*/m, '' );

	attrs.push( [ attr_name, attr_value ] );
    }

    return attrs;
}

function process_tag_img( src )
{
    var tag = src.match( /<\s*(img)\s*([^>]*)\s*>/i );
    var elem = tag[1];
    var attrs = parse_attrs( tag[2] );

    warn( 'Element: '+elem );
    for ( var i=0 ; i < attrs.length; i++ ) {
	warn( '\t    '+attrs[i][0]+' = '+attrs[i][1] );
    }
}

var file = application.args[0];
var text = System.readFile( file );
var imgs = text.match( /<\s*img\s*[^>]*\s*>/ig );

for ( var i=0 ; i < imgs.length ; i++ ) {
    process_tag_img( imgs[i] );
}