md2conf 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99aae9ed537d6bbd435e310f1d104bb83c4cb013
4
- data.tar.gz: c6c2dbfb7ab2bc0bc84627a7440785251c6106a8
3
+ metadata.gz: a0f39ff071ace36b7bf5019c84e20247c1eb563e
4
+ data.tar.gz: 39c3ce8db54264dff21f95c0c7c997ae0fd5751d
5
5
  SHA512:
6
- metadata.gz: 249cc7658acf867f02cf85e878cc187562aa276c185132f2fa0eaa19b3f6b694a1494fd66e6d28b64047157bc7b539a2b782da499bbaf01956ccc47e8d9c5e5e
7
- data.tar.gz: c2c54d26b897c73a699681c90f285dd12c3aedc5808ad06ad18bea910d584bf3ad7d69d8242dc9fff30f8f7ecaeb7ac5cfb59209095938d3a59f8f61748e47de
6
+ metadata.gz: c48b4f6bd990331be451810c312a027a4648000f54121480ca21806924c8f22535b1a0b3ca3b014bb29d9f987e256dc26ae7a9fa596d8ee420ecbee8f36b98f8
7
+ data.tar.gz: fdcdaa8f0abe0d9069a080bb1731a5fb67200387af1e2f72c571942923f696bd392768fbe4cf0ebd682e5fdbf368cbdf2703a91a0c92e42cf30fd8f50f083237
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.2.0] - 2017-08-29
8
+ ### Added
9
+ - Macro support. Any macro can be specified in the form of `{MACRO:ARGUMENT}` inside your Markdown files.
10
+ Check out the updated README for more info.
11
+
12
+
7
13
  ## [0.1.2] - 2017-08-21
8
14
  ### Changed
9
15
  - Updated documentation.
data/README.md CHANGED
@@ -24,6 +24,8 @@ $ gem install md2conf
24
24
 
25
25
  ## Usage
26
26
 
27
+ ### Basic usage
28
+
27
29
  ```ruby
28
30
  require 'md2conf'
29
31
 
@@ -32,6 +34,38 @@ conf_xhtml = Md2conf.parse_markdown(File.read('./README.md'))
32
34
 
33
35
  Contents of `conf_xhtml` is now ready to be pushed to Confluence.
34
36
 
37
+ ### Custom macros
38
+
39
+ It is possible to set up custom macros to be processed inside your Markdown files.
40
+
41
+ Macros must be specified in the format `{MACRO_NAME:MACRO_ARG}` and reside outside of code blocks.
42
+
43
+ Macro definition are read from the file `~/.md2conf.yaml` inside a `macros` key:
44
+
45
+ ```yaml
46
+ macros:
47
+ PUP: <a href="https://tickets.puppetlabs.com/browse/PUP-%<arg>s"><img src="https://img.shields.io/badge/PUP-%<arg>s-blue.svg" /></a>
48
+ PUPTEXT: <a href="https://tickets.puppetlabs.com/browse/PUP-%<arg>s">PUP-%<arg>s</a>
49
+ ```
50
+
51
+ Having these definitions, the following macros:
52
+
53
+ ```markdown
54
+ {PUP:7123} Fixed a bug
55
+
56
+ {PUPTEXT:7885} Work in progress
57
+ ```
58
+
59
+ will be converted to this:
60
+
61
+ ```html
62
+ <a href="https://tickets.puppetlabs.com/browse/PUP-7123"><img src="https://img.shields.io/badge/PUP-7123-blue.svg" /></a> Fixed a bug
63
+
64
+ <a href="https://tickets.puppetlabs.com/browse/PUP-7885">PUP-7885</a> Work in progress
65
+ ```
66
+
67
+ Note the usage of `%<arg>s` - these will be replaced by the `MACRO_ARG` part that is specified inside each macro.
68
+
35
69
  ## Development
36
70
 
37
71
  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.
@@ -40,4 +74,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
40
74
 
41
75
  ## Contributing
42
76
 
43
- Bug reports and pull requests are welcome on GitHub at https://github.com/pegasd/md2conf.
77
+ Bug reports, feature and pull requests are welcome at the official GitHub [repo](https://github.com/pegasd/md2conf).
@@ -1,6 +1,7 @@
1
1
  require 'md2conf/version'
2
2
  require 'redcarpet'
3
3
  require 'cgi'
4
+ require 'yaml'
4
5
 
5
6
  # Processes markdown and converts it to Confluence Storage Format.
6
7
  #
@@ -11,13 +12,18 @@ module Md2conf
11
12
  class ConfluenceUtil
12
13
  # @param [String] html XHTML rendered by redcarpet gem (must have fenced code blocks).
13
14
  # @param [Integer] max_toc_level Table of Contents maximum header depth.
14
- def initialize(html, max_toc_level)
15
+ def initialize(html, max_toc_level, config_file)
15
16
  @html = html
16
17
  @max_toc_level = max_toc_level
18
+ return unless File.file?(File.expand_path(config_file))
19
+ @config = YAML.load_file(File.expand_path(config_file))
20
+ return unless @config.key? 'macros'
21
+ @macros = @config['macros']
17
22
  end
18
23
 
19
24
  # Launch all internal parsers.
20
25
  def parse
26
+ process_macros if @macros
21
27
  process_mentions
22
28
  convert_info_macros
23
29
  process_code_blocks
@@ -26,6 +32,38 @@ module Md2conf
26
32
  @html
27
33
  end
28
34
 
35
+ # Process custom macros. Macro definitions should be placed in `~/.md2conf.yaml`.
36
+ # Format is described in the README.
37
+ #
38
+ # Macros are blocks that are contained in curly braces like this: `{JIRA:52837}`.
39
+ def process_macros
40
+ html_new = ''
41
+ last_position = 0
42
+ @html.scan(/{(.*?)}/) do |macro|
43
+ next if inside_code_block Regexp.last_match.pre_match
44
+ macro_name = macro.first.split(':')[0]
45
+ macro_arg = macro.first.split(':')[1]
46
+
47
+ confluence_code = if @macros.include? macro_name
48
+ @macros[macro_name] % { arg: macro_arg }
49
+ else
50
+ "<code>#{macro.first}</code>"
51
+ end
52
+
53
+ since_last_match = @html[last_position..Regexp.last_match.begin(0) - 1]
54
+ html_new << "#{since_last_match}#{confluence_code}"
55
+ last_position = Regexp.last_match.end(1)
56
+ end
57
+
58
+ # Did we have at least one match?
59
+ return unless Regexp.last_match
60
+ @html = html_new << if inside_code_block Regexp.last_match.pre_match
61
+ @html[last_position..-1]
62
+ else
63
+ Regexp.last_match.post_match
64
+ end
65
+ end
66
+
29
67
  # Process username mentions.
30
68
  #
31
69
  # Everything that starts with an `@` and is not enclosed in a code block will be converted
@@ -51,20 +89,6 @@ module Md2conf
51
89
  end
52
90
  end
53
91
 
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
92
  # Convert Info macros to Confluence-friendly format:
69
93
  #
70
94
  # @example Regular informational message
@@ -138,6 +162,21 @@ module Md2conf
138
162
  #{@html}
139
163
  HTML
140
164
  end
165
+
166
+ private
167
+
168
+ # Check whether we're inside a code block based on pre_match variable.
169
+ def inside_code_block(pre_match)
170
+ # *
171
+ return false unless pre_match.include? '<code'
172
+
173
+ # <code> *
174
+ return true unless pre_match.include? '</code>'
175
+
176
+ # <code></code> *
177
+ # <code></code><code> *
178
+ pre_match.rindex('<code') > pre_match.rindex('</code>')
179
+ end
141
180
  end
142
181
 
143
182
  # @example Just read a Markdown file and parse it
@@ -148,14 +187,14 @@ module Md2conf
148
187
  # @param [Integer] max_toc_level Table of Contents maximum header depth.
149
188
  #
150
189
  # @return [String] Confluence Storage Format document.
151
- def self.parse_markdown(markdown, cut_header: true, max_toc_level: 7)
190
+ def self.parse_markdown(markdown, cut_header: true, max_toc_level: 7, config_file: '~/.md2conf.yaml')
152
191
  if cut_header && markdown.start_with?('# ')
153
192
  markdown = markdown.lines.drop(1).join
154
193
  end
155
194
 
156
195
  md = Redcarpet::Markdown.new(Redcarpet::Render::XHTML.new, tables: true, fenced_code_blocks: true, autolink: true)
157
196
  html = md.render(markdown)
158
- confluence = ConfluenceUtil.new(html, max_toc_level)
197
+ confluence = ConfluenceUtil.new(html, max_toc_level, config_file)
159
198
  confluence.parse
160
199
  end
161
200
  end
@@ -1,3 +1,3 @@
1
1
  module Md2conf
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.2.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: md2conf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Piven
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-08-21 00:00:00.000000000 Z
12
+ date: 2017-08-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -89,8 +89,6 @@ extensions: []
89
89
  extra_rdoc_files: []
90
90
  files:
91
91
  - CHANGELOG.md
92
- - CODE_OF_CONDUCT.md
93
- - CONTRIBUTING.md
94
92
  - README.md
95
93
  - lib/md2conf.rb
96
94
  - lib/md2conf/version.rb
@@ -1,74 +0,0 @@
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/
@@ -1,3 +0,0 @@
1
- # Contribution Guide
2
-
3
- Feel free to contribute!