From 105f9e9759ca5cab6ca37b0c75ad0e56afbd5834 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Thu, 29 Dec 2016 13:50:42 +0100 Subject: [PATCH] =?UTF-8?q?Outils=20de=20conversion=20Trac=20=E2=86=92=20M?= =?UTF-8?q?arkdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HowtoTracToMarkdown.md | 143 ++++++++++++++++++++++++++++++++--------- 1 file changed, 114 insertions(+), 29 deletions(-) diff --git a/HowtoTracToMarkdown.md b/HowtoTracToMarkdown.md index a08a2b85..1405fcc9 100644 --- a/HowtoTracToMarkdown.md +++ b/HowtoTracToMarkdown.md @@ -8,44 +8,67 @@ Si on souhaite convertir des pages du format Trac vers le format Markdown un pet #!/usr/bin/env ruby ## Based on: https://gist.github.com/619537 -body = STDIN.read +class TracToMarkdown -# convert inline code blocs -body.gsub!(/\{\{\{([^\n]+?)\}\}\}/, '`\1`') + def convert(input) + output = input.clone -body.gsub!(/^\s*\*/, '*') + # convert inline code blocs + output.gsub!(/\{\{\{([^\n]+?)\}\}\}/, '`\1`') -# add a line break before lists -body.gsub!(/(\S)\n\* /, '\1' "\n\n* ") + # remove spaces before list items + output.gsub!(/^\s*\*/, '*') -body.gsub!(/^[ ]*\{\{\{/, '{{{') -body.gsub!(/(\S)\n\{\{\{/, '\1' "\n\n{{{") -body.gsub!(/\{\{\{(.+?)\}\}\}/m) { |m| - m.each_line.map { |x| - x.gsub(/ *[\{\}]{3}/,"~~~") - }.join -} + # add a line break before lists + output.gsub!(/(^[^\* ].*)\n\* /, '\1'+"\n\n* ") -# 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') + output.gsub!(/^[ ]*\{\{\{/, '{{{') + output.gsub!(/(\S)\n\{\{\{/, '\1 '+"\n\n{{{") + output.gsub!(/\{\{\{(.+?)\}\}\}/m) { |m| + m.each_line.map { |x| + x.gsub(/ *[\{\}]{3}/,"~~~") + }.join + } -# convert links -body.gsub!(/\[(https?[^\s\[\]]+)\s([^\[\]]+)\]/, '[\2](\1)') -body.gsub!(/([^(])(https?[^\s\[\]]+)/, '\1<\2>') -body.gsub!(/\!(([A-Z][a-z0-9]+){2,})/, '\1') + # convert headings syntax + output.gsub!(/\=\=\=\=\=\s(.+?)\s\=\=\=\=\=/, '##### \1') + output.gsub!(/\=\=\=\=\s(.+?)\s\=\=\=\=/, '#### \1') + output.gsub!(/\=\=\=\s(.+?)\s\=\=\=/, '### \1') + output.gsub!(/\=\=\s(.+?)\s\=\=/, '## \1') + output.gsub!(/\=\s(.+?)\s\=/, '# \1') -body.gsub!(/'''(.+?)'''/, '*\1*') -body.gsub!(/''(.+?)''/, '_\1_') -body.gsub!(/^\s\d\./, '1.') + # convert links + output.gsub!(/\[(https?[^\s\[\]]+)\s([^\[\]]+)\]/, '[\2](\1)') + output.gsub!(/([^(])(https?[^\s\[\]]+)/, '\1<\2>') + output.gsub!(/\!(([A-Z][a-z0-9]+){2,})/, '\1') -# custom add-on for pedantic satisfaction -body.gsub!(/ *(\.){3,}/, '…') + output.gsub!(/'''(.+?)'''/, '*\1*') + output.gsub!(/''(.+?)''/, '_\1_') + output.gsub!(/^\s\d\./, '1.') -STDOUT.write(body) + # custom add-on for pedantic satisfaction + # output.gsub!(/ ?(\.){3,}/, '…') + + output + end + + def remove_outline(input) + input.gsub(/^\[\[PageOutline\]\]\s+/, "") + end + + def add_trac_banner(input) + banner = "**Cette page a été importée automatiquement de notre ancien wiki mais n'a pas encore été révisée.**" + + banner + "\n\n" + input + end + +end + +converter = TracToMarkdown.new + +output = converter.convert(STDIN.read) + +STDOUT.write(output) ~~~ Copiez simplement ce script là où vous le souhaitez, rendez-le exécutable puis : @@ -55,3 +78,65 @@ $ 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 :) + +Voici le script qui a servi à convertir tout le contenu du Trac (nécessite la gem `Mechanize`) : + +~~~{.ruby} +require 'mechanize' +require 'net/http' +require_relative "./trac_to_markdown" + +domain = "http://trac.evolix.net" +index_url = "#{domain}/infogerance/wiki/TitleIndex" + +base_pattern = %r(/infogerance/wiki/) +links = Mechanize.new.get(index_url).links_with(href: base_pattern) + +skip_pages = [ + %r(/infogerance/wiki/TracGuide), + %r(/infogerance/wiki/TitleIndex), + %r(/infogerance/wiki/CamelCase), + %r(/infogerance/wiki/RecentChange), + %r(/infogerance/wiki/TitleIndex.*), + %r(/infogerance/wiki/Wiki.*), + %r(/infogerance/wiki/InterWiki), + %r(/infogerance/wiki/InterTrac), + %r(/infogerance/wiki/InterMapTxt), + %r(/infogerance/wiki/SandBox), + %r(/infogerance/wiki/Trac[A-Z]\w*), +] + +skip_content = /ATTENTION, CETTE PAGE EST OBSOLÈTE/ + +save_path = Pathname.new(".") +# save_path = Pathname.new("../wiki") + +converter = TracToMarkdown.new + +links.each do |link| + page = link.href + + if skip_pages.any? { |skip_page| skip_page.match(page) } + puts " #{page} skipped" + next + end + + file_path = save_path.join(page.gsub(base_pattern, "") + ".md") + file_path.dirname.mkpath + + uri = URI.parse(domain + page + "?format=txt") + trac_content = Net::HTTP.get(uri).force_encoding("UTF-8") + + if skip_content.match(trac_content) + puts " #{page} skipped" + next + end + + md_content = converter.convert(trac_content) + md_content = converter.remove_outline(md_content) + md_content = converter.add_trac_banner(md_content) + + file_path.write(md_content) + puts "+ #{page} saved" +end +~~~