#!/usr/bin/env ruby Header = %Q(\
).split("\n") Footer = %Q(\ ).split("\n") Source = '/home/kik/documents/cours/brut/' Destination = '/home/kik/documents/cours/html/' def parser_ligne(ligne, jour) balise = case ligne when /^ *TITRE/ then "h#{$'[0,1]}" when /^ *EM/ then "em" else "p" end ligne.gsub!(/TITRE./, "") ligne.gsub!(/EM/, "") ligne.gsub!(/<, "«") ligne.gsub!(/>>/, "»") # traitement des notes while ligne[/ \(\d\) /] ligne = $` + " [" + $&[2,1] + "] " + $' end if ligne[/^ *NOTE/] ligne = "" + "#{$'[0,1]}"+ $'.gsub(/^./, "") end # traitement des autres liens while ligne[/HREF=\S+/] match, pre, post = $&, $`, $' reste = post.split('/A') ligne = pre + "" + reste[0] + "" + reste[1] end return (ligne[/^ *\n$/] ? "" : ligne.gsub(/^/, "<#{balise}>").gsub(/\n$/, "#{balise}>\n") ) end def parser_fichier(flux) jour = File.basename(flux.path) texte_html = [""] while (ligne = flux.gets) texte_html << parser_ligne(ligne, jour) end return texte_html end Dir.new(Source).each { |dossier| unless dossier == '.' or dossier == '..' liens_dates = [] matiere = Source + '/' + dossier fichiers = Dir.new(matiere).entries.delete_if { |f| f[/\./] }.delete_if { |f| f[/\.html$/] }.sort puts fichiers.inspect # debug texte_html = [] fichiers.each { |f| flux = File.new( matiere + '/' + f ) texte_html += parser_fichier(flux) liens_dates.push(File.basename(flux.path)) flux.close } texte_href = [""] liens_dates.each { |nom| texte_href[0] += " | #{nom}" } %x(mkdir -p #{Destination}) unless File.exist?(Destination) texte_html = Header + texte_href + texte_html + texte_href + Footer fichier_html = File.new(Destination + dossier + ".html", "w") texte_html.each { |ligne| fichier_html.puts(ligne) } fichier_html.close end } puts "terminé."