🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: es.comp.os.linux.programacion
Article #565

Re: Script para Reemplazar pero con peculiaridades

#565
From: heltena
Date: Tue, 06 Mar 2007 16:11
81 lines
3375 bytes
On Mar 6, 11:12 am, dios.nag...@gmail.com wrote:
> Hola a todos!
>
> Estoy intentando hacer un script para pasar mis webs a w3c. Ya tengo
> todo bien hecho, no obstante me queda un tema pendiente y me gustaria
> saber si me podriais ayudar o mostrar algun ejemplo dado que no
> encontre ningun script para orientarme.
>
> Tengo varias webs en HTML, php, y demas, y queria coger lo que incluya
> el campo ALT de una imagen, y crear un nuevo campo TITLE que contenga
> lo que tenga ALT en esa linea. Basicamente seria esto:
>
> Tengo:
>
> <img src="pepito.jpg" ALT="foto de pepito">
>
> y querria que el script me hiciera:
>
> <img src="pepito.jpg" ALT="foto de pepito" TITLE="foto de pepito">
>
> Es esto posible???

En cinco minutos y con python:

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#!/usr/bin/env python

import sgmllib
import sys

class Parser(sgmllib.SGMLParser):
	def __init__(self, file):
		sgmllib.SGMLParser.__init__(self)
		self.file = file

	def unknown_starttag(self, tag, attr):
		self.file.write("<%s" % tag)
		for k, v in attr:
			self.file.write("%s=\"%s\"" % (k, v))
		self.file.write(">")

	def unknown_endtag(self, tag):
		self.file.write("</%s>" % tag)

	def handle_data(self, data):
		self.file.write(data)

	def start_img(self, attr):
		self.file.write("<img")
		title = None
		has_title = False
		need_space = False
		for k, v in attr:
			if need_space:
				self.file.write(" ")
			self.file.write("%s=\"%s\"" % (k, v))
			need_space = True
			if k == 'alt':
				title = v
			elif k == 'title':
				has_title = True
		if not has_title and title is not None:
			self.file.write(" title=\"%s\"" % title)
		self.file.write(">")

def main(ffrom, fto):
	parser = Parser(open(fto, "wt"))
	parser.feed(open(ffrom).read())
	parser.close()

if __name__ == "__main__":
	if len(sys.argv) != 3:
		print >>sys.stderr, "%s filename-from filename-to" % sys.argv[0]
	else:
		main(sys.argv[1], sys.argv[2])
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

No me hago responsable de que no chute.

--
Helio Tejedor
Technical Details
Message-ID: <1173197490.188286.36230@j27g2000cwj.googlegroups.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!j27g2000cwj.googlegroups.com!not-for-mail
References: <1173175969.375077.8140@p10g2000cwp.googlegroups.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: f86c8,bb6ab5d2b43b8c9b
X-Google-Attributes: gidf86c8,public
X-Google-Language: SPANISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news2.google.com!postnews.google.com!j27g2000cwj.googlegroups.com!not-for-mail
From: "heltena" <heltena@gmail.com>
Newsgroups: es.comp.os.linux.programacion
Subject: Re: Script para Reemplazar pero con peculiaridades
Date: 6 Mar 2007 08:11:30 -0800
Organization: http://groups.google.com
Lines: 82
Message-ID: <1173197490.188286.36230@j27g2000cwj.googlegroups.com>
References: <1173175969.375077.8140@p10g2000cwp.googlegroups.com>
NNTP-Posting-Host: 193.147.222.244
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1173197507 23219 127.0.0.1 (6 Mar 2007 16:11:47 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 6 Mar 2007 16:11:47 +0000 (UTC)
In-Reply-To: <1173175969.375077.8140@p10g2000cwp.googlegroups.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.1) Gecko/20061205 Iceweasel/2.0.0.1 (Debian-2.0.0.1+dfsg-2),gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: j27g2000cwj.googlegroups.com; posting-host=193.147.222.244;
   posting-account=ljKoDA0AAABEvzHlRrQ7YAIM9PPVxdEL