Code de la page : «Delxml2html»

Retour

Ceci est le code php du fichier :


<?php
$head['css'][0]['filename']='code';
$titre='Delxml2html';
require'../autres/source-haut.php'?>
<h1>Utiliser le programme "delxml2html".</h1>
<p class="pintro">Ce programme vous  permet, si vous avez un compte del.icio.us,  de mettre tous vos marques pages en une seule page html, que vous  pouvez
  ensuite mettre sur votre site.
  <br />  J'ai rédigé quelques instructions pour montrer  comment  utiliser le programme, réalisé par
  Sébastien Sauvage : <a href="http://www.sebsauvage.net/" hreflang="fr">http://www.sebsauvage.net/</a>.
</p>
<h2 id="instructions">Instructions</h2>
<ol>
  <li>Rendez-vous sur 
  <a href="http://del.icio.us/api/posts/all/" hreflang="en">http://del.icio.us/api/posts/all/</a></li>
  <li>Entrez votre login et votre mot de passe del.icio.us</li>
  <li>Enregistrez la page sous le nom "all.xml" (appuyez sur [Ctrl] + S ou allez dans le menu Fichier &gt; Enregistrer sous)</li>
  <li>Enregistrez le programme dans le m&ecirc;me dossier que le fichier "all.xml"</li>
  <li>Lancez le programme</li>
  <li>Si vous utilisez le programme modifié, enregistrez le  fichier CSS dans un fichier nommé "favs.css" que vous placerez dans le m&ecirc;me répertoire que "favs.html"</li>
  <li>C'est bon ! Vous avez votre page favs.html, avec tous vos favoris en un seule page</li>
  <li>Mettez &agrave; jour de temps en temps en ré-enregistrant le fichier "all.xml"</li>
</ol>
<h2 id="code">Code du programme</h2>
<p>Programme original (<a  id="codeoriginal" href="delxmloriginal.py">Télécharger au format Python</a>) (<a hreflang="fr" href="http://sebsauvage.net/python/programs.html#delxml2html">http://sebsauvage.net/python/programs.html#delxml2html</a>) :
</p>
<pre>#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
import datetime, time
import xml.dom.minidom
# 
# delxml2html.py  version 1.0.0
# del.icio.us XML export to HTML converter.
#
# This program takes the XML export of your bookmarks
# and creates an HTML page from it.
#
# Instructions:
# 
#   1) Go to http://del.icio.us/api/posts/all
#   2) Enter your del.icio.us login and password
#   3) Save the page as all.xml
#   4) Run this program
#   5) You have your bookmarks in favs.html
#
# Requirements:
#     - a del.icio.us account.
#     - Python 2.4 or later.
#
# License: This program is public domain.
#
# Author: Sébastien SAUVAGE (webmaster of http://sebsauvage.net)
#
print 'Reading all.xml and writing favs.html...'
document = xml.dom.minidom.parse('all.xml')
attrv = document.getElementsByTagName('posts')[0].attributes['update'].value
export_date = attrv.replace('T',' ').replace('Z','')[:10]
posts = {}
# Get all posts, put them in a dictionnary (key = date/time of post) 
for post in document.getElementsByTagName('post'):
    timep = post.attributes['time'].value.replace('T',' ').replace('Z','')
    attributes = {'time':timep}
    for attributename in ('href','description','extended','tag'):
        attributes[attributename] = u""
        try:
            attributes[attributename] = post.attributes[attributename].value
        except KeyError:
            pass  # Value not found. Nevermind.
    # Strip ridiculously long page titles:
    attributes['description'] = attributes['description'][:150]
    posts[timep] = attributes
    
# Take the list of posts (chronological order) and build HTML
htmlbody = u""
for timep in reversed(sorted(posts.keys())):
    htmlbody += (u'&lt;a href="%(href)s"&gt;&lt;b&gt;%(description)s&lt;/b&gt;&lt;/a&gt; - &lt;small&gt;'
                +u'%(href)s&lt;/small&gt;&lt;br&gt;&lt;dd&gt;%(extended)s (Tags: %(tag)s)&lt;/dd&gt;'
                +u'&lt;br&gt;&lt;br&gt;\n') % posts[timep]

htmlout=u'''&lt;html&gt;&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;style type="text/css"&gt;&lt;!--body{
font-family:"Trebuchet MS",Verdana,Arial,Helvetica,sans-serif;font-size:10pt;}
--&gt;&lt;/style&gt; &lt;title&gt;Bookmarks %s&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;Bookmarks %s&lt;/h1&gt;&lt;body&gt;
%s
&lt;/body&gt;
&lt;/html&gt;
''' % (export_date,export_date,htmlbody)

file = open('favs.html','w+b')
file.write(htmlout.encode('UTF-8'))
file.close()

print "Done."</pre>
<p id="codemodifie">Programme modifié (<a href="delxml2html.py">Télécharger au format Python</a>) :<br />
Modifications apportées :
</p>
<ul>
  <li>Respect des normes du W3C</li>
  <li>Version xhtml1.0 strict</li>
  <li>Utilisation d'une feuile de style CSS</li>
</ul>
<pre>#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
import datetime, time
import xml.dom.minidom
# 
# delxml2html.py  version 1.0.0
# del.icio.us XML export to HTML converter.
#
# This program takes the XML export of your bookmarks
# and creates an HTML page from it.
#
# Instructions:
# 
#   1) Go to http://del.icio.us/api/posts/all
#   2) Enter your del.icio.us login and password
#   3) Save the page as all.xml
#   4) Run this program
#   5) You have your bookmarks in favs.html
#
# Requirements:
#     - a del.icio.us account.
#     - Python 2.4 or later.
#
# License: This program is public domain.
#
# Author: Sébastien SAUVAGE (webmaster of http://sebsauvage.net)
#
# Ce programme a été modifé.
#
# Modifications apportées :
#
#   -Respect des normes du W3C
#   -Version xhtml 1.0 Strict
#   -Utilisation d'une feuille de style CSS.
#
print 'Reading all.xml and writing favs.html...'
document = xml.dom.minidom.parse('all.xml')
attrv = document.getElementsByTagName('posts')[0].attributes['update'].value
export_date = attrv.replace('T',' ').replace('Z','')[:10]
posts = {}
# Get all posts, put them in a dictionnary (key = date/time of post) 
for post in document.getElementsByTagName('post'):
    timep = post.attributes['time'].value.replace('T',' ').replace('Z','')
    attributes = {'time':timep}
    for attributename in ('href','description','extended','tag'):
        attributes[attributename] = u""
        try:
            attributes[attributename] = post.attributes[attributename].value
        except KeyError:
            pass  # Value not found. Nevermind.
    # Strip ridiculously long page titles:
    attributes['description'] = attributes['description'][:150]
    posts[timep] = attributes
    
# Take the list of posts (chronological order) and build HTML
htmlbody = u""
for timep in reversed(sorted(posts.keys())):
    htmlbody += (u'&lt;span class="d"&gt;&lt;a href="%(href)s"&gt;%(description)s&lt;/a&gt;&lt;/span&gt; - &lt;span class="l"&gt;'
                +u'%(href)s&lt;/span&gt;&lt;br /&gt;&lt;span class="n"&gt;%(extended)s (Tags: %(tag)s)&lt;/span&gt;'
                +u'&lt;br /&gt;&lt;br /&gt;\n') % posts[timep]

htmlout=u'''
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr"&gt;
   &lt;head&gt;
       &lt;title&gt;Bookmarks %s&lt;/title&gt;
       &lt;meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" /&gt;
       &lt;link type="text/css" href="favs.css" rel="stylesheet" /&gt;
   &lt;/head&gt;
   &lt;body&gt;    &lt;h1&gt;Bookmarks %s&lt;/h1&gt;
    &lt;p&gt;
    %s
    &lt;/p&gt;
    &lt;/body&gt;
    &lt;/html&gt;
''' % (export_date,export_date,htmlbody)
file = open('favs.html','w+b')
file.write(htmlout.encode('UTF-8'))
file.close()

print "Done."</pre>
<p id="codecss">Et le fichier CSS qui va avec le programme modifié :</p>
<pre>body{font-family:"Trebuchet MS",Verdana,Arial,Helvetica,sans-serif;font-size:10pt;}
p span.d{font-weight:bold;}
p span.l{font-size:0.8em;}
p span.n{margin-left:3%;}</pre>
<?php require'../autres/source-bas.php'?>

Retour