-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmd2confl.rb
executable file
·86 lines (67 loc) · 2.97 KB
/
md2confl.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env ruby
# encoding: utf-8
require 'confluence-soap'
require 'markdown2confluence'
require 'optparse'
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: md2confl.rb [options...] -s <SPACE_NAME> -i <PAGE_ID>\nassumes defaults that can be set in options parsing..."
options[:pageId] = nil
opts.on('-i', '--pageId PAGE_ID', 'REQUIRED. The Confluence page id to upload the converted markdown to.') do |pageId|
options[:pageId] = pageId
end
options[:spaceName] = nil
opts.on('-s', '--space SPACE_NAME', 'REQUIRED. The Confluence space name in which the page resides.') do |space|
options[:spaceName] = space
end
# set default for Markdown file name and path
options[:markdownFile] = 'README.md'
opts.on( '-f', '--markdownFile FILE', "Path to the Markdown file to convert and upload. Defaults to '#{options[:markdownFile]}'") do |file|
options[:markdownFile] = file
end
# set default for Confluence server
options[:server] = 'http://confluence.example.com'
opts.on( '-c', '--server CONFLUENCE_SERVER', "The Confluence server to upload to. Defaults to '#{options[:server]}'") do |server|
options[:server] = server
end
options[:user] = nil
opts.on('-u', '--user USER', 'The Confluence user. Can also be specified by the \'CONFLUENCE_USER\' environment variable.') do |pageId|
options[:user] = pageId
end
options[:password] = nil
opts.on('-p', '--password PASSWORD', 'The Confluence user\'s password. Can also be specified by the \'CONFLUENCE_PASSWORD\' environment variable.') do |pageId|
options[:password] = pageId
end
options[:verbose] = false
opts.on('-v', '--verbose', 'Output more information') do
options[:verbose] = true
end
opts.on('-h', '--help', 'Display this screen') do
puts opts
exit
end
end
optparse.parse!
# space_name and page_id are required arguments
raise OptionParser::MissingArgument, '-s SPACE_NAME is a required argument' if options[:spaceName].nil?
raise OptionParser::MissingArgument, '-p PAGE_ID is a required argument' if options[:pageId].nil?
user = ENV['CONFLUENCE_USER'] || options[:user] || ''
password = ENV['CONFLUENCE_PASSWORD'] || options[:password] || ''
opts = options[:verbose] ? {} : {log: false}
cs = ConfluenceSoap.new("#{options[:server]}/rpc/soap-axis/confluenceservice-v2?wsdl", user, password, opts)
pages = cs.get_pages(options[:spaceName])
uploader_page = pages.detect { |page| page.id == options[:pageId] }
if uploader_page.nil?
puts "exiting... could not find pageId: #{options[:pageId]}"
exit
end
begin
text = File.read(options[:markdownFile])
@convertedText = "#{Kramdown::Document.new(text).to_confluence}"
rescue Exception => ex
warn "There was an error running the converter: \n#{ex}"
end
@convertedText = "#{@convertedText}\n\n(rendered at #{Time.now.getutc} by md2confl)"
uploader_page.content = cs.convert_wiki_to_storage_format(@convertedText)
options = {minorEdit: true, versionComment: 'updated by md2confl'}
cs.update_page(uploader_page)