conversion-mediawiki-to-bbcode.py
Cet article a été mis à jour, vous consultez ici une archive de cet article!
Code PYTHON :
# Ce programme python contient dans le meme repertoire : # texte a convertir.txt : le code mediawiki a traduire # texte converti.txt : le bbcode cms obtenu après exécution du script B=[] B =B + ['', '', '|frameless', ''] # termes parasites B =B + ['', '', '|left', ''] # à supprimer B =B + ['', '', '[[Catégorie:', ']]'] # Ligne inutile B =B + [' ----- ', '', '=====', ''] # Toujours la plus longue chaine en 1er B =B + [' ---- ', '', '====', ''] # pour éviter la recursivite B =B + [' --- ', '', '===', ''] B =B + [' -- ', '', '==', ''] B =B + ['-- ', '', '-- ', ''] # mise en forme B =B + [' --', '', ' --', ''] # des chapitres(ajout espace avant/après) B =B + ['[code]', '[/code]', '<nowiki>', '</nowiki>'] B =B + ['[youtube=560,315]https://www.youtube.com/watch?v=', '[/youtube]', '[https://www.youtube.com/watch?v=', ' Voir la vidéo]'] B =B + ['[dm]', '[/dm]', '[https://www.dailymotion.com/video/', ' Voir la vidéo]'] B =B + ['[vimeo]', '[/vimeo]', '[https://vimeo.com/', ' Voir la vidéo]'] B =B + ['[size=', ']', '<p style="font-size:', ';">'] B =B + ['[/size]', '', '</p>', ''] B =B + ['[quote]', '', '<blockquote>', ''] B =B + ['[/quote]', '', '</blockquote>', ''] B =B + ['[b]', '[/b]', "'''", "'''"] B =B + ['[i]', '[/i]', "''", "''"] B =B + ['[u]', '[/u]', '<u>', '</u>'] B =B + ['[color=#', ']', '<span style="color:#', ';">'] B =B + ['[/color]', '', '</span>', ''] B =B + [':D', '', '[[image:icon_biggrin.gif]]', ''] B =B + [':)', '', '[[image:icon_smile.gif]]', ''] B =B + [':(', '', '[[image:icon_sad.gif]]', ''] B =B + [':shock:', '','[[image:icon_eek.gif]]', ''] B =B + ['8)', '', '[[image:icon_cool.gif]]', ''] B =B + [':lol:', '', '[[image:icon_lol.gif]]', ''] B =B + [':mad:', '', '[[image:icon_mad.gif]]', ''] B =B + [':P', '', '[[image:icon_razz.gif]]', ''] B =B + [':oops:', '', '[[image:icon_redface.gif]]', ''] B =B + [':o', '', '[[image:icon_surprised.gif]]', ''] B =B + [':cry', '', '[[image:icon_cry.gif]]', ''] B =B + [':evil:', '', '[[image:icon_evil.gif]]', ''] B =B + [':twisted:', '', '[[image:icon_twisted.gif]]', ''] B =B + [':roll:', '', '[[image:icon_rolleyes.gif]]', ''] B =B + [':wink:', '', '[[image:icon_wink.gif]]', ''] B =B + [':!:', '', '[[image:icon_exclaim.gif]]', ''] B =B + [' :???: ', '', '[[image:icon_confused2.gif]]', ''] B =B + [':?:', '', '[[image:icon_question.gif]]', ''] B =B + [':?', '', '[[image:icon_confused.gif]]', ''] B =B + [':idea:', '', '[[image:icon_idea.gif]]', ''] B =B + [':arrow:', '', '[[image:icon_arrow.gif]]', ''] B =B + [':|', '', '[[image:icon_neutral.gif]]', ''] B =B + [':mrgreen:', '', '[[image:icon_mrgreen.gif]]', ''] B =B + [':hang:', '', '[[image:icon_hangloose.gif]]', ''] B =B + [':/: ', '', '[[image:icon_unhappy.gif]]', ''] B =B + [' :zyeuxkipic: ', '', '[[image:icon_exorbite.gif]]', ''] B =B + [' :help ', '', '[[image:icon_helpme.gif]]', ''] B =B + [' :love ', '', '[[image:icon_love.gif]]', ''] B =B + [' :mdr-f ', '', '[[image:icon_mdr-f.gif]]', ''] B =B + [' :bravo ', '', '[[image:icon_bravo.gif]]', ''] B =B + [' :tongue ', '', '[[image:icon_tongue.gif]]', ''] B =B + [' :jmincline ', '', '[[image:icon_remarquable.gif]]', ''] B =B + [' :gna: ', '', '[[image:icon_neztourne-f.gif]]', ''] B =B + [' :excitee: ', '', '[[image:icon_excitee.gif]]', ''] B =B + [' :ptdr ', '', '[[image:icon_ptdr.gif]]', ''] B =B + [' :pong ', '', '[[image:icon_pong.gif]]', ''] B =B + [' :meaculpa ', '', '[[image:icon_mea-culpa.gif]]', ''] B =B + [' :top: ', '', '[[image:icon_top.gif]]', ''] B =B + [' :dcroises ', '', '[[image:icon_jcroiselesdoigts.gif]]', ''] B =B + [' :respect ', '', '[[image:icon_respect.gif]]', ''] B =B + [' :hangf: ', '', '[[image:icon_hangloose-f.gif]]', ''] B =B + [' :sante ', '', '[[image:Icon_ahoy.gif]]', ''] B =B + [' :tchin ', '', '[[image:Icon_tchin.gif]]', ''] B =B + [' :ahyo ', '', '[[image:Icon_ahoy2.gif]]', ''] B =B + [' :joke ', '', '[[image:icon_joke.gif]]', ''] B =B + [' :cartonjaune: ', '', '[[image:Icon_foot-carton-jaune.gif]]', ''] B =B + [' :cartonrouge ', '', '[[image:Icon_foot-carton-rouge.gif]]', ''] B =B + [' :nrv ', '', '[[image:icon_nrv.gif]]', ''] B =B + [' :furax ', '', '[[image:icon_furax.gif]]', ''] B =B + [' :idea ', '', '[[image:icon_idea2.gif]]', ''] B =B + [' :wtf ', '', '[[image:icon_wtf.gif]]', ''] B =B + [' :think ', '', '[[image:icon_think.gif]]', ''] B =B + [' :cramer ', '', '[[image:Icon_Cramer.gif]]', ''] B =B + [' :CramerJ ', '', '[[image:Icon_CramerJ.gif]]', ''] B =B + [' :merci ', '', '[[image:Icon_pancarte-merci.gif]]', ''] B =B + [' :+1: ', '', '[[image:Icon_pancarte-plus1.gif]]', ''] B =B + [' :poups ', '', '[[image:Icon_pancarte-oops.png]]', ''] B =B + [' :tusort ', '', '[[image:Icon_pancarte-tusort.gif]]', ''] B =B + [' :tgenial ', '', '[[image:icon_tgenial.gif]]', ''] B =B + [' :upup ', '', '[[image:Icon_pancarte-upup.gif]]', ''] B =B + [' :gne? ', '', '[[image:Icon_pancarte-gne.gif]]', ''] B =B + [' :bienvwenue: ', '', '[[image:Icon_bienvwenue.gif]]', ''] B =B + [' :vatependre ', '', '[[image:Icon_pancarte-va-te-pendre.gif]]', ''] B =B + [' :fuck ', '', '[[image:Icon_pancarte-fuck.gif]]', ''] B =B + [' :dejadehors ', '', '[[image:Icon_pancarte-deja-dehors.gif]]', ''] B =B + [' :heh-hee:', '', '[[image:icon_heh-hee.gif]]', ''] B =B + ['[img]/upload/', '[/img]', '[[image:', ']]'] B =B + ['[url]http', '[/url]', '[http', ']'] B =B + ['[link]', '[/link]', '[[', ']]'] try: # la conversion peut commencer ! destination = open('texte converti.txt', "w") with open('texte a convertir.txt','r')as source: nblignevide=0 for ligne in source: A=(ligne.strip('\n')) if A=='': nblignevide= nblignevide +1 else: nblignevide =0 # recherche mots clé for i in range(0,len(B),4): for j in range(0,10): C=A.find(B[i+2]) # 1er mot clé if C<0: # introuvable dans cette ligne break if len(B[i+3])>0: D=A.find(B[i+3],C+1) # 2e mot clé? else: D=-1 if D<0: A=A[:C]+B[i]+A[C+len(B[i+2]):] #un seul remplacement else: # 2 remplacements A=A[:C]+B[i]+A[C+len(B[i+2]):D]+B[i+1]+A[D+len(B[i+3]):] # Cas particuliers if B[i+2]=='[http': #recherche lien texte E=A.find(" ",C+1) if E<(D+4): # si le texte existe A=A[:C+4]+'='+A[C+5:]# remplacer [ par = A=A[:E]+']'+A[E+1:]# remplacer espace par ] if B[i+2]=='[[Catégorie:': #supprimer la catégorie A=A[:C]+ A[D:] if nblignevide< 2: destination.write(A+"\n") #print (A) destination.close() print('Conversion terminée !') print('Le résultat de la conversion se trouve dans le fichier suivant :') print('"texte converti.txt"') print except: print('Le fichier à traiter:') print('"texte a convertir.txt"') print('est introuvable. Il doit être dans le même répertoire que ce programme !') print