HowtoTracToMarkdown: commentaires + optimisation
This commit is contained in:
parent
caff5a686e
commit
90c1121ebb
|
@ -10,20 +10,34 @@ Si on souhaite convertir des pages du format Trac vers le format Markdown un pet
|
||||||
|
|
||||||
body = STDIN.read
|
body = STDIN.read
|
||||||
|
|
||||||
|
# convert inline code blocs
|
||||||
body.gsub!(/\{\{\{([^\n]+?)\}\}\}/, '`\1`')
|
body.gsub!(/\{\{\{([^\n]+?)\}\}\}/, '`\1`')
|
||||||
|
|
||||||
body.gsub!(/^\s*\*/, '*')
|
body.gsub!(/^\s*\*/, '*')
|
||||||
|
|
||||||
|
# add a line break before lists
|
||||||
body.gsub!(/(\S)\n\* /, '\1' "\n\n* ")
|
body.gsub!(/(\S)\n\* /, '\1' "\n\n* ")
|
||||||
|
|
||||||
body.gsub!(/^[ ]*\{\{\{/, '{{{')
|
body.gsub!(/^[ ]*\{\{\{/, '{{{')
|
||||||
body.gsub!(/(\S)\n\{\{\{/, '\1' "\n\n{{{")
|
body.gsub!(/(\S)\n\{\{\{/, '\1' "\n\n{{{")
|
||||||
body.gsub!(/\{\{\{(.+?)\}\}\}/m){|m| m.each_line.map{|x| "#{x}".gsub(/ *[\{\}]{3}/,"~~~")}.join}
|
body.gsub!(/\{\{\{(.+?)\}\}\}/m) { |m|
|
||||||
|
m.each_line.map { |x|
|
||||||
|
x.gsub(/ *[\{\}]{3}/,"~~~")
|
||||||
|
}.join
|
||||||
|
}
|
||||||
|
|
||||||
|
# convert headings syntax
|
||||||
body.gsub!(/\=\=\=\=\=\s(.+?)\s\=\=\=\=\=/, '##### \1')
|
body.gsub!(/\=\=\=\=\=\s(.+?)\s\=\=\=\=\=/, '##### \1')
|
||||||
body.gsub!(/\=\=\=\=\s(.+?)\s\=\=\=\=/, '#### \1')
|
body.gsub!(/\=\=\=\=\s(.+?)\s\=\=\=\=/, '#### \1')
|
||||||
body.gsub!(/\=\=\=\s(.+?)\s\=\=\=/, '### \1')
|
body.gsub!(/\=\=\=\s(.+?)\s\=\=\=/, '### \1')
|
||||||
body.gsub!(/\=\=\s(.+?)\s\=\=/, '## \1')
|
body.gsub!(/\=\=\s(.+?)\s\=\=/, '## \1')
|
||||||
body.gsub!(/\=\s(.+?)\s\=/, '# \1')
|
body.gsub!(/\=\s(.+?)\s\=/, '# \1')
|
||||||
|
|
||||||
|
# convert links
|
||||||
body.gsub!(/\[(https?[^\s\[\]]+)\s([^\[\]]+)\]/, '[\2](\1)')
|
body.gsub!(/\[(https?[^\s\[\]]+)\s([^\[\]]+)\]/, '[\2](\1)')
|
||||||
body.gsub!(/([^(])(https?[^\s\[\]]+)/, '\1<\2>')
|
body.gsub!(/([^(])(https?[^\s\[\]]+)/, '\1<\2>')
|
||||||
body.gsub!(/\!(([A-Z][a-z0-9]+){2,})/, '\1')
|
body.gsub!(/\!(([A-Z][a-z0-9]+){2,})/, '\1')
|
||||||
|
|
||||||
body.gsub!(/'''(.+?)'''/, '*\1*')
|
body.gsub!(/'''(.+?)'''/, '*\1*')
|
||||||
body.gsub!(/''(.+?)''/, '_\1_')
|
body.gsub!(/''(.+?)''/, '_\1_')
|
||||||
body.gsub!(/^\s\d\./, '1.')
|
body.gsub!(/^\s\d\./, '1.')
|
||||||
|
@ -40,4 +54,4 @@ Copiez simplement ce script là où vous le souhaitez, rendez-le exécutable pui
|
||||||
$ cat Page.trac | trac2md
|
$ cat Page.trac | trac2md
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
*Note* : le script ne gère pas une conversion parfaite, il faudra relire le document et faire quelques corrections manuelles (titre en "front matter Yaml", etc.). Patches welcome :)
|
*Note* : le script ne gère pas une conversion parfaite, il faudra relire le document et faire quelques corrections manuelles (titre en "front matter Yaml", etc.). Patches welcome :)
|
||||||
|
|
Loading…
Reference in a new issue