md2conf 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 99aae9ed537d6bbd435e310f1d104bb83c4cb013
4
+ data.tar.gz: c6c2dbfb7ab2bc0bc84627a7440785251c6106a8
5
+ SHA512:
6
+ metadata.gz: 249cc7658acf867f02cf85e878cc187562aa276c185132f2fa0eaa19b3f6b694a1494fd66e6d28b64047157bc7b539a2b782da499bbaf01956ccc47e8d9c5e5e
7
+ data.tar.gz: c2c54d26b897c73a699681c90f285dd12c3aedc5808ad06ad18bea910d584bf3ad7d69d8242dc9fff30f8f7ecaeb7ac5cfb59209095938d3a59f8f61748e47de
@@ -0,0 +1,16 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [0.1.2] - 2017-08-21
8
+ ### Changed
9
+ - Updated documentation.
10
+
11
+ ### Fixed
12
+ - Info macros substitution did not always work.
13
+
14
+ ## [0.1.0] - 2017-08-21
15
+ ### Added
16
+ - Initial release.
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at vtyshkevich@iponweb.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,3 @@
1
+ # Contribution Guide
2
+
3
+ Feel free to contribute!
@@ -0,0 +1,43 @@
1
+ # Md2conf
2
+
3
+ Confverts Markdown to Confluence storage format
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'md2conf'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install md2conf
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require 'md2conf'
29
+
30
+ conf_xhtml = Md2conf.parse_markdown(File.read('./README.md'))
31
+ ```
32
+
33
+ Contents of `conf_xhtml` is now ready to be pushed to Confluence.
34
+
35
+ ## Development
36
+
37
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pegasd/md2conf.
@@ -0,0 +1,161 @@
1
+ require 'md2conf/version'
2
+ require 'redcarpet'
3
+ require 'cgi'
4
+
5
+ # Processes markdown and converts it to Confluence Storage Format.
6
+ #
7
+ # The main workload is done by redcarpet gem, which generates an almost ready-to-use
8
+ # XHTML output. But there's some more magic performed by ConfluenceUtil class afterwards.
9
+ module Md2conf
10
+ # ConfluenceUtil class contains various helpers for processing XHTML generated by redcarpet gem.
11
+ class ConfluenceUtil
12
+ # @param [String] html XHTML rendered by redcarpet gem (must have fenced code blocks).
13
+ # @param [Integer] max_toc_level Table of Contents maximum header depth.
14
+ def initialize(html, max_toc_level)
15
+ @html = html
16
+ @max_toc_level = max_toc_level
17
+ end
18
+
19
+ # Launch all internal parsers.
20
+ def parse
21
+ process_mentions
22
+ convert_info_macros
23
+ process_code_blocks
24
+ add_toc
25
+
26
+ @html
27
+ end
28
+
29
+ # Process username mentions.
30
+ #
31
+ # Everything that starts with an `@` and is not enclosed in a code block will be converted
32
+ # to a valid Confluence username.
33
+ def process_mentions
34
+ html_new = ''
35
+ last_position = 0
36
+ @html.scan(/@(\w+)/) do |mention|
37
+ next if inside_code_block Regexp.last_match.pre_match
38
+
39
+ confluence_code = "<ac:link><ri:user ri:username=\"#{mention.first}\"/></ac:link>"
40
+ since_last_match = @html[last_position..Regexp.last_match.begin(0) - 1]
41
+ html_new << "#{since_last_match}#{confluence_code}"
42
+ last_position = Regexp.last_match.end(1)
43
+ end
44
+
45
+ # Did we have at least one match?
46
+ return unless Regexp.last_match
47
+ @html = html_new << if inside_code_block Regexp.last_match.pre_match
48
+ @html[last_position..-1]
49
+ else
50
+ Regexp.last_match.post_match
51
+ end
52
+ end
53
+
54
+ private
55
+ # Check whether we're inside a code block based on pre_match variable.
56
+ def inside_code_block(pre_match)
57
+ # *
58
+ return false unless pre_match.include? '<code'
59
+
60
+ # <code> *
61
+ return true unless pre_match.include? '</code>'
62
+
63
+ # <code></code> *
64
+ # <code></code><code> *
65
+ pre_match.rindex('<code') > pre_match.rindex('</code>')
66
+ end
67
+
68
+ # Convert Info macros to Confluence-friendly format:
69
+ #
70
+ # @example Regular informational message
71
+ # > My info message
72
+ #
73
+ # @example A Note (`Note: ` will be removed from the message)
74
+ # > Note: An important note
75
+ #
76
+ # @example A Warning (`Warning: ` will be removed from the message)
77
+ # > Warning: An extremely important warning
78
+ def convert_info_macros
79
+ confluence_code = <<~HTML
80
+ <ac:structured-macro ac:name="%<macro_name>s">
81
+ <ac:rich-text-body>
82
+ %<quote>s
83
+ </ac:rich-text-body>
84
+ </ac:structured-macro>
85
+ HTML
86
+
87
+ @html.scan(%r{<blockquote>(.*?)</blockquote>}m).each do |quote|
88
+ quote = quote.first
89
+ if quote.include? 'Note: '
90
+ quote_new = quote.strip.sub 'Note: ', ''
91
+ macro_name = 'note'
92
+ elsif quote.include? 'Warning: '
93
+ quote_new = quote.strip.sub 'Warning: ', ''
94
+ macro_name = 'warning'
95
+ else
96
+ quote_new = quote.strip
97
+ macro_name = 'info'
98
+ end
99
+ @html.sub! "<blockquote>#{quote}</blockquote>", format(confluence_code, macro_name: macro_name, quote: quote_new)
100
+ end
101
+ end
102
+
103
+ # Convert regular code blocks to Confluence code blocks.
104
+ # Language type is also supported.
105
+ #
106
+ # `puppet` is currently replaced by `ruby` language type.
107
+ def process_code_blocks
108
+ @html.scan(%r{<pre><code.*?>.*?</code></pre>}m).each do |codeblock|
109
+ content = codeblock.match(%r{<pre><code.*?>(.*?)</code></pre>}m)[1]
110
+ lang = codeblock.match(/code class="(.*)"/)
111
+ lang = if lang.nil?
112
+ 'none'
113
+ else
114
+ lang[1].sub('puppet', 'ruby')
115
+ end
116
+
117
+ confluence_code = <<~HTML
118
+ <ac:structured-macro ac:name="code">
119
+ <ac:parameter ac:name="theme">RDark</ac:parameter>
120
+ <ac:parameter ac:name="linenumbers">true</ac:parameter>
121
+ <ac:parameter ac:name="language">#{lang}</ac:parameter>
122
+ <ac:plain-text-body><![CDATA[#{CGI.unescape_html content}]]></ac:plain-text-body>
123
+ </ac:structured-macro>
124
+ HTML
125
+
126
+ @html.sub! codeblock, confluence_code
127
+ end
128
+ end
129
+
130
+ # Add Table of Contents Confluence tag at the beginning.
131
+ #
132
+ # Use @max_toc_level class variable to specify maximum header depth.
133
+ def add_toc
134
+ @html = <<~HTML
135
+ <ac:structured-macro ac:name="toc">
136
+ <ac:parameter ac:name="maxLevel">#{@max_toc_level}</ac:parameter>
137
+ </ac:structured-macro>
138
+ #{@html}
139
+ HTML
140
+ end
141
+ end
142
+
143
+ # @example Just read a Markdown file and parse it
144
+ # Md2conf.parse_markdown File.read './README.md'
145
+ #
146
+ # @param [String] markdown Markdown contents to convert to Confluence format.
147
+ # @param [Boolean] cut_header Whether to cut off initial header (must start with `/^# /`).
148
+ # @param [Integer] max_toc_level Table of Contents maximum header depth.
149
+ #
150
+ # @return [String] Confluence Storage Format document.
151
+ def self.parse_markdown(markdown, cut_header: true, max_toc_level: 7)
152
+ if cut_header && markdown.start_with?('# ')
153
+ markdown = markdown.lines.drop(1).join
154
+ end
155
+
156
+ md = Redcarpet::Markdown.new(Redcarpet::Render::XHTML.new, tables: true, fenced_code_blocks: true, autolink: true)
157
+ html = md.render(markdown)
158
+ confluence = ConfluenceUtil.new(html, max_toc_level)
159
+ confluence.parse
160
+ end
161
+ end
@@ -0,0 +1,3 @@
1
+ module Md2conf
2
+ VERSION = '0.1.2'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: md2conf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Eugene Piven
8
+ - Vladimir Tyshkevich
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2017-08-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rubocop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: redcarpet
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '2.0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '2.0'
84
+ description:
85
+ email:
86
+ - epiven@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - CHANGELOG.md
92
+ - CODE_OF_CONDUCT.md
93
+ - CONTRIBUTING.md
94
+ - README.md
95
+ - lib/md2conf.rb
96
+ - lib/md2conf/version.rb
97
+ homepage: https://github.com/pegasd/md2conf
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.6.12
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Convert Markdown to Confluence XHTML storage format
121
+ test_files: []