pandoc_object_filters 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rubocop.yml +20 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +40 -0
  9. data/Rakefile +10 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/examples/README.md +5 -0
  13. data/examples/caps-sample.md +2 -0
  14. data/examples/caps.rb +10 -0
  15. data/examples/comments-sample.md +9 -0
  16. data/examples/comments.rb +21 -0
  17. data/examples/deflists-sample.md +20 -0
  18. data/examples/deflists.rb +24 -0
  19. data/examples/format-sample.md +1 -0
  20. data/examples/format.rb +15 -0
  21. data/examples/metavars-sample.md +7 -0
  22. data/examples/metavars.rb +27 -0
  23. data/lib/pandoc_object_filters.rb +21 -0
  24. data/lib/pandoc_object_filters/element.rb +135 -0
  25. data/lib/pandoc_object_filters/element/attr.rb +43 -0
  26. data/lib/pandoc_object_filters/element/base.rb +44 -0
  27. data/lib/pandoc_object_filters/element/base_element.rb +11 -0
  28. data/lib/pandoc_object_filters/element/block.rb +8 -0
  29. data/lib/pandoc_object_filters/element/document.rb +19 -0
  30. data/lib/pandoc_object_filters/element/enum.rb +15 -0
  31. data/lib/pandoc_object_filters/element/inline.rb +8 -0
  32. data/lib/pandoc_object_filters/element/meta.rb +18 -0
  33. data/lib/pandoc_object_filters/element/meta_value.rb +8 -0
  34. data/lib/pandoc_object_filters/element/target.rb +10 -0
  35. data/lib/pandoc_object_filters/filter.rb +37 -0
  36. data/lib/pandoc_object_filters/version.rb +3 -0
  37. data/lib/pandoc_object_filters/walker.rb +64 -0
  38. data/pandoc_object_filters.gemspec +28 -0
  39. metadata +138 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ddff0d01d96a620197b61aeb75d689bae00af8e8
4
+ data.tar.gz: 19cc75ca102c7ba5322a9218107161c98ea2973a
5
+ SHA512:
6
+ metadata.gz: 0490b5ffa632824523e681687d59a570b0c3caa5a20870ebaa07ce3409fbe47ffebf7cf3fcb11466f1d606558abe1e659b3077b96cd58bf974b379b8d72cc86a
7
+ data.tar.gz: 053e6714be757739144b3ef773655a42c32a55af536af00433ea7d26194c41902068542869ba5c69174be313aa33c3913f4c4641edbabe10f12a24230f607564
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,20 @@
1
+ AllCops:
2
+ Exclude:
3
+ - bin/**/*
4
+ - examples/**/*
5
+ - test/**/*
6
+ - tmp/**/*
7
+ DisplayCopNames: true
8
+ TargetRubyVersion: 2.2
9
+
10
+ Metrics/LineLength:
11
+ Max: 120
12
+
13
+ Style/SignalException:
14
+ EnforcedStyle: only_raise
15
+
16
+ Style/SpaceInsideBlockBraces:
17
+ EnforcedStyleForEmptyBraces: space
18
+
19
+ Style/StringLiterals:
20
+ EnforcedStyle: double_quotes
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ before_install: gem install bundler -v 1.13.2
@@ -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 mjstone@on-site.com. 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/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pandoc_object_filters.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Mike Virata-Stone
4
+ Copyright (c) 2015 Tom Potts
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # PandocObjectFilters
2
+
3
+ This is a simple gem that is a fork of
4
+ https://github.com/karaken12/pandoc-filters-ruby but providing Pandoc filters
5
+ via objects. It's possible this gem may get deprecated if the functionality is
6
+ merged into the `pandoc-filter` gem.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "pandoc_object_filters"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install pandoc_object_filters
23
+
24
+ ## Usage
25
+
26
+ For now, see the [examples](examples).
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ 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).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/smellsblue/pandoc_object_filters. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pandoc_object_filters"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ # Examples
2
+ These sample filters are reworked versions of the Python examples.
3
+ See [John MacFarlane](https://github.com/jgm)'s
4
+ [examples](https://github.com/jgm/pandocfilters/tree/master/examples)
5
+ for the originals.
@@ -0,0 +1,2 @@
1
+
2
+ This is the caps sample with Äüö.
data/examples/caps.rb ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "pandoc_object_filters"
4
+
5
+ # Pandoc filter to convert all regular text to uppercase.
6
+ # Code, link URLs, etc. are not affected.
7
+
8
+ PandocObjectFilters.filter do |element|
9
+ element.value.upcase! if element.is_a?(PandocObjectFilters::Element::Str)
10
+ end
@@ -0,0 +1,9 @@
1
+ Regular text with Äüö.
2
+
3
+ <!-- BEGIN COMMENT -->
4
+
5
+ This is a comment with Äüö
6
+
7
+ <!-- END COMMENT -->
8
+
9
+ This is regular text again.
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "pandoc_object_filters"
4
+
5
+ incomment = false
6
+
7
+ PandocObjectFilters.filter! do |element|
8
+ if element.is_a?(PandocObjectFilters::Element::RawBlock)
9
+ if element.format == "html"
10
+ if /<!-- BEGIN COMMENT -->/.match(element.value)
11
+ incomment = true
12
+ next []
13
+ elsif /<!-- END COMMENT -->/.match(element.value)
14
+ incomment = false
15
+ next []
16
+ end
17
+ end
18
+ end
19
+
20
+ next [] if incomment
21
+ end
@@ -0,0 +1,20 @@
1
+ Some Definitions
2
+
3
+ Term 1
4
+
5
+ : Definition 1
6
+
7
+ Term 2 with *inline markup*
8
+
9
+ : Definition 2
10
+
11
+ { some code, part of Definition 2 }
12
+
13
+ Third paragraph of definition 2.
14
+
15
+ Term with Äüö
16
+
17
+ : Definition with Äüö
18
+
19
+
20
+ Regular Text.
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "pandoc_object_filters"
4
+
5
+ def self.tobullet(term, defs)
6
+ elements = [PandocObjectFilters::Element::Para.new([PandocObjectFilters::Element::Strong.new(term)])]
7
+ defs.each do |el|
8
+ el.each do |el_el|
9
+ elements.push(el_el)
10
+ end
11
+ end
12
+ elements
13
+ end
14
+
15
+ def self.bullet_list(items)
16
+ items = items.map { |item| tobullet(item[0], item[1]) }
17
+ PandocObjectFilters::Element::BulletList.new(items)
18
+ end
19
+
20
+ PandocObjectFilters.filter! do |element|
21
+ if element.is_a?(PandocObjectFilters::Element::DefinitionList)
22
+ bullet_list(element.elements)
23
+ end
24
+ end
@@ -0,0 +1 @@
1
+ This document was converted to %{format}
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Pandoc filter to allow inserting the format that the document was formatted
4
+ # to. %{format} will be replaced by the format that pandoc passes in to this
5
+ # filter. It will only be replaced from matching Str elements.
6
+
7
+ require "pandoc_object_filters"
8
+
9
+ filter = PandocObjectFilters::Filter.new
10
+
11
+ filter.filter do |element|
12
+ if element.is_a?(PandocObjectFilters::Element::Str) && element.value == "%{format}"
13
+ element.value = filter.format
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ ---
2
+ author: Caleb Hyde
3
+ ---
4
+
5
+ # %{author}
6
+
7
+ This was written by %{author}
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Pandoc filter to allow interpolation of metadata fields
4
+ # into a document. %{fields} will be replaced by the field's
5
+ # value, assuming it is of the type MetaInlines or MetaString.
6
+
7
+ require "pandoc_object_filters"
8
+
9
+ filter = PandocObjectFilters::Filter.new
10
+
11
+ filter.filter! do |element|
12
+ if element.is_a?(PandocObjectFilters::Element::Str)
13
+ match = /%\{(.*)\}$/.match(element.value)
14
+
15
+ if match
16
+ field = match[1]
17
+ result = filter.meta[field]
18
+
19
+ if result.is_a?(PandocObjectFilters::Element::MetaInlines)
20
+ attr = PandocObjectFilters::Element::Attr.build(classes: ["interpolated"], key_values: { "field" => field })
21
+ next PandocObjectFilters::Element::Span.new([attr, result.elements])
22
+ elsif result.is_a?(PandocObjectFilters::Element::MetaString)
23
+ next PandocObjectFilters::Element::Str.new(result.value)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ # Copyright (c) On-Site, 2016
2
+ # Copyright (c) Tom Potts, 2015
3
+ # Inspired by Python code by John MacFarlane.
4
+ # See http://pandoc.org/scripting.html
5
+ # and https://github.com/jgm/pandocfilters
6
+ # for more information.
7
+
8
+ module PandocObjectFilters
9
+ autoload :Element, "pandoc_object_filters/element"
10
+ autoload :Filter, "pandoc_object_filters/filter"
11
+ autoload :VERSION, "pandoc_object_filters/version"
12
+ autoload :Walker, "pandoc_object_filters/walker"
13
+
14
+ def self.filter(input = $stdin, output = $stdout, argv = ARGV, &block)
15
+ PandocObjectFilters::Filter.new(input, output, argv, &block).filter
16
+ end
17
+
18
+ def self.filter!(input = $stdin, output = $stdout, argv = ARGV, &block)
19
+ PandocObjectFilters::Filter.new(input, output, argv, &block).filter!
20
+ end
21
+ end
@@ -0,0 +1,135 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ autoload :Attr, "pandoc_object_filters/element/attr"
6
+ autoload :Base, "pandoc_object_filters/element/base"
7
+ autoload :BaseElement, "pandoc_object_filters/element/base_element"
8
+ autoload :Block, "pandoc_object_filters/element/block"
9
+ autoload :Document, "pandoc_object_filters/element/document"
10
+ autoload :Enum, "pandoc_object_filters/element/enum"
11
+ autoload :Inline, "pandoc_object_filters/element/inline"
12
+ autoload :Meta, "pandoc_object_filters/element/meta"
13
+ autoload :MetaValue, "pandoc_object_filters/element/meta_value"
14
+ autoload :Target, "pandoc_object_filters/element/target"
15
+
16
+ def self.to_ast(object)
17
+ if object.respond_to?(:to_ast)
18
+ object.to_ast
19
+ elsif object.is_a?(Array)
20
+ object.map { |x| to_ast(x) }
21
+ elsif object.is_a?(Hash)
22
+ result = {}
23
+ object.each { |key, value| result[key] = to_ast(value) }
24
+ result
25
+ else
26
+ object
27
+ end
28
+ end
29
+
30
+ def self.to_object(object)
31
+ if object.is_a?(Array)
32
+ object.map { |x| to_object(x) }
33
+ elsif object.is_a?(Hash) && object.include?("t") && object.include?("c")
34
+ raise "Unknown type: #{object['t']}" unless PandocObjectFilters::Element.const_defined?(object["t"])
35
+ type = PandocObjectFilters::Element.const_get(object["t"])
36
+ raise "Invalid type: #{object['t']}" unless type < PandocObjectFilters::Element::BaseElement
37
+ type.new(to_object(object["c"]))
38
+ elsif object.is_a?(Hash) && object.include?("unMeta")
39
+ PandocObjectFilters::Element::Meta.new(to_object(object["unMeta"]))
40
+ elsif object.is_a?(Hash)
41
+ result = {}
42
+ object.each { |key, value| result[key] = to_object(value) }
43
+ result
44
+ else
45
+ object
46
+ end
47
+ end
48
+
49
+ def self.walk(object, &block)
50
+ PandocObjectFilters::Walker.new(object, &block).walk
51
+ end
52
+
53
+ def self.walk!(object, &block)
54
+ PandocObjectFilters::Walker.new(object, &block).walk!
55
+ end
56
+
57
+ # rubocop:disable Metrics/LineLength
58
+ [["MetaMap", :elements, { include: [PandocObjectFilters::Element::MetaValue, PandocObjectFilters::Element::Enum] }],
59
+ ["MetaList", :elements, { include: [PandocObjectFilters::Element::MetaValue, PandocObjectFilters::Element::Enum] }],
60
+ ["MetaBool", :value, { include: [PandocObjectFilters::Element::MetaValue] }],
61
+ ["MetaString", :value, { include: [PandocObjectFilters::Element::MetaValue] }],
62
+ ["MetaInlines", :elements, { include: [PandocObjectFilters::Element::MetaValue, PandocObjectFilters::Element::Enum] }],
63
+ ["MetaBlocks", :elements, { include: [PandocObjectFilters::Element::MetaValue, PandocObjectFilters::Element::Enum] }],
64
+ ["Plain", :elements, { include: [PandocObjectFilters::Element::Block, PandocObjectFilters::Element::Enum] }],
65
+ ["Para", :elements, { include: [PandocObjectFilters::Element::Block, PandocObjectFilters::Element::Enum] }],
66
+ ["CodeBlock", :attributes, :value, { include: [PandocObjectFilters::Element::Block], conversions: { attributes: PandocObjectFilters::Element::Attr } }],
67
+ ["RawBlock", :format, :value, { include: [PandocObjectFilters::Element::Block] }],
68
+ ["BlockQuote", :elements, { include: [PandocObjectFilters::Element::Block, PandocObjectFilters::Element::Enum] }],
69
+ ["OrderedList", :attributes, :elements, { include: [PandocObjectFilters::Element::Block, PandocObjectFilters::Element::Enum] }],
70
+ ["BulletList", :elements, { include: [PandocObjectFilters::Element::Block, PandocObjectFilters::Element::Enum] }],
71
+ ["DefinitionList", :elements, { include: [PandocObjectFilters::Element::Block, PandocObjectFilters::Element::Enum] }],
72
+ ["Header", :level, :attributes, :elements, { include: [PandocObjectFilters::Element::Block, PandocObjectFilters::Element::Enum], conversions: { attributes: PandocObjectFilters::Element::Attr } }],
73
+ ["HorizontalRule", { include: [PandocObjectFilters::Element::Block] }],
74
+ ["Table", :captions, :alignments, :widths, :headers, :rows, { include: [PandocObjectFilters::Element::Block] }],
75
+ ["Div", :attributes, :elements, { include: [PandocObjectFilters::Element::Block, PandocObjectFilters::Element::Enum], conversions: { attributes: PandocObjectFilters::Element::Attr } }],
76
+ ["Null", { include: [PandocObjectFilters::Element::Block] }],
77
+ ["Str", :value, { include: [PandocObjectFilters::Element::Inline] }],
78
+ ["Emph", :elements, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum] }],
79
+ ["Strong", :elements, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum] }],
80
+ ["Strikeout", :elements, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum] }],
81
+ ["Superscript", :elements, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum] }],
82
+ ["Subscript", :elements, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum] }],
83
+ ["SmallCaps", :elements, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum] }],
84
+ ["Quoted", :type, :elements, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum] }],
85
+ ["Cite", :citations, :elements, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum] }],
86
+ ["Code", :attributes, :value, { include: [PandocObjectFilters::Element::Inline], conversions: { attributes: PandocObjectFilters::Element::Attr } }],
87
+ ["Space", { include: [PandocObjectFilters::Element::Inline] }],
88
+ ["SoftBreak", { include: [PandocObjectFilters::Element::Inline] }],
89
+ ["LineBreak", { include: [PandocObjectFilters::Element::Inline] }],
90
+ ["Math", :type, :value, { include: [PandocObjectFilters::Element::Inline] }],
91
+ ["RawInline", :format, :value, { include: [PandocObjectFilters::Element::Inline] }],
92
+ ["Link", :attributes, :elements, :target, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum], conversions: { attributes: PandocObjectFilters::Element::Attr, target: PandocObjectFilters::Element::Target } }],
93
+ ["Image", :attributes, :elements, :target, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum], conversions: { attributes: PandocObjectFilters::Element::Attr, target: PandocObjectFilters::Element::Target } }],
94
+ ["Note", :elements, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum] }],
95
+ ["Span", :attributes, :elements, { include: [PandocObjectFilters::Element::Inline, PandocObjectFilters::Element::Enum], conversions: { attributes: PandocObjectFilters::Element::Attr } }]].each do |name, *params|
96
+ # rubocop:enable Metrics/LineLength
97
+ name.freeze
98
+
99
+ options =
100
+ if params.last.is_a?(Hash)
101
+ params.pop
102
+ else
103
+ {}
104
+ end
105
+
106
+ const_set(name, Class.new(PandocObjectFilters::Element::BaseElement) do
107
+ (options[:include] || []).each { |mod| include mod }
108
+
109
+ if params.size == 1
110
+ contents_attr params.first
111
+ else
112
+ params.each_with_index { |param, index| contents_attr param, index }
113
+ end
114
+
115
+ define_method(:element_name) { name }
116
+
117
+ if options[:conversions]
118
+ private
119
+
120
+ define_method(:convert_contents) do
121
+ @contents = @contents.map.with_index do |x, index|
122
+ convert_to_type = options[:conversions][params[index]]
123
+
124
+ if convert_to_type && !x.is_a?(convert_to_type)
125
+ convert_to_type.new(x)
126
+ else
127
+ x
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end)
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,43 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ class Attr < PandocObjectFilters::Element::Base
6
+ contents_attr :identifier, 0
7
+ contents_attr :classes, 1
8
+ contents_attr :key_values, 2
9
+
10
+ def self.build(options = {})
11
+ id = options.fetch(:identifier, "")
12
+ classes = options.fetch(:classes, [])
13
+ key_values = options.fetch(:key_values, [])
14
+
15
+ key_values = key_values.to_a if key_values.is_a?(Hash)
16
+
17
+ new([id, classes, key_values])
18
+ end
19
+
20
+ def [](key)
21
+ # NOTE: While this pseudo Hash implementations are inefficient, they
22
+ # guarantee any changes to key_values will be honored, which would be
23
+ # difficult if the key_values were cached in a Hash
24
+ result = key_values.find { |pair| pair.first == key } || []
25
+ result[1]
26
+ end
27
+
28
+ def []=(key, value)
29
+ found = key_values.find { |pair| pair.first == key }
30
+
31
+ if found
32
+ found[1] = value
33
+ else
34
+ key_values << [key, value]
35
+ end
36
+ end
37
+
38
+ def include?(key)
39
+ !!key_values.find { |pair| pair.first == key }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,44 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ class Base
6
+ attr_accessor :contents
7
+
8
+ def self.contents_attr(name, index = nil)
9
+ if index
10
+ define_method(name) { contents[index] }
11
+ define_method("#{name}=") { |value| contents[index] = value }
12
+ else
13
+ define_method(name) { contents }
14
+ define_method("#{name}=") { |value| @contents = value }
15
+ end
16
+ end
17
+
18
+ def initialize(contents = [])
19
+ @contents = contents
20
+ convert_contents if respond_to?(:convert_contents, true)
21
+ end
22
+
23
+ def to_ast
24
+ PandocObjectFilters::Element.to_ast(contents)
25
+ end
26
+
27
+ def inspect
28
+ to_ast.inspect
29
+ end
30
+
31
+ def ==(other)
32
+ self.class == other.class && contents == other.contents
33
+ end
34
+
35
+ def walk(&block)
36
+ PandocObjectFilters::Element.walk(self, &block)
37
+ end
38
+
39
+ def walk!(&block)
40
+ PandocObjectFilters::Element.walk!(self, &block)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ class BaseElement < PandocObjectFilters::Element::Base
6
+ def to_ast
7
+ { "t" => element_name, "c" => PandocObjectFilters::Element.to_ast(contents) }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ module Block
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,19 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ class Document < PandocObjectFilters::Element::Base
6
+ attr_reader :meta
7
+
8
+ def initialize(ast)
9
+ object = PandocObjectFilters::Element.to_object(ast)
10
+ @meta = object[0]
11
+ @contents = object[1]
12
+ end
13
+
14
+ def to_ast
15
+ [meta.to_ast, PandocObjectFilters::Element.to_ast(contents)]
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ module Enum
6
+ def [](key)
7
+ elements[key]
8
+ end
9
+
10
+ def []=(key, value)
11
+ elements[key] = value
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,8 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ module Inline
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ class Meta < PandocObjectFilters::Element::Base
6
+ include PandocObjectFilters::Element::Enum
7
+ alias elements contents
8
+
9
+ def initialize(contents = {})
10
+ super
11
+ end
12
+
13
+ def to_ast
14
+ { "unMeta" => PandocObjectFilters::Element.to_ast(contents) }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ module MetaValue
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ module Element
5
+ class Target < PandocObjectFilters::Element::Base
6
+ contents_attr :url, 0
7
+ contents_attr :title, 1
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,37 @@
1
+ require "pandoc_object_filters"
2
+ require "json"
3
+
4
+ module PandocObjectFilters
5
+ class Filter
6
+ attr_accessor :doc, :format, :meta
7
+
8
+ def initialize(input = $stdin, output = $stdout, argv = ARGV, &block)
9
+ @input = input
10
+ @output = output
11
+ @argv = argv
12
+ @block = block
13
+ end
14
+
15
+ def filter(&block)
16
+ process(block) do
17
+ PandocObjectFilters::Element.walk(@doc, &@block)
18
+ @doc
19
+ end
20
+ end
21
+
22
+ def filter!(&block)
23
+ process(block) { PandocObjectFilters::Element.walk!(@doc, &@block) }
24
+ end
25
+
26
+ private
27
+
28
+ def process(block)
29
+ @block = block unless @block
30
+ @doc = PandocObjectFilters::Element::Document.new(JSON.parse(@input.read))
31
+ @format = @argv.first
32
+ @meta = @doc.meta
33
+ result = yield
34
+ @output.puts JSON.dump(PandocObjectFilters::Element.to_ast(result))
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module PandocObjectFilters
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,64 @@
1
+ require "pandoc_object_filters"
2
+
3
+ module PandocObjectFilters
4
+ class Walker
5
+ def initialize(object, &block)
6
+ @object = object
7
+ @block = block
8
+ end
9
+
10
+ def walk(object = @object)
11
+ if object.is_a?(Array)
12
+ object.each do |item|
13
+ if item.is_a?(PandocObjectFilters::Element::BaseElement)
14
+ @block.call(item)
15
+ end
16
+
17
+ walk(item)
18
+ end
19
+ elsif object.is_a?(Hash)
20
+ object.values.each do |value|
21
+ walk(value)
22
+ end
23
+ elsif object.is_a?(PandocObjectFilters::Element::Base)
24
+ walk(object.contents)
25
+ end
26
+
27
+ object
28
+ end
29
+
30
+ def walk!(object = @object)
31
+ if object.is_a?(Array)
32
+ result = []
33
+ object.each do |item|
34
+ if item.is_a?(PandocObjectFilters::Element::BaseElement)
35
+ res = @block.call(item)
36
+ if !res
37
+ result.push(walk!(item))
38
+ elsif res.is_a?(Array)
39
+ res.each do |z|
40
+ result.push(walk!(z))
41
+ end
42
+ else
43
+ result.push(walk!(res))
44
+ end
45
+ else
46
+ result.push(walk!(item))
47
+ end
48
+ end
49
+ return result
50
+ elsif object.is_a?(Hash)
51
+ result = {}
52
+ object.each do |key, value|
53
+ result[key] = walk!(value)
54
+ end
55
+ return result
56
+ elsif object.is_a?(PandocObjectFilters::Element::Base)
57
+ object.contents = walk!(object.contents)
58
+ return object
59
+ else
60
+ return object
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "pandoc_object_filters/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pandoc_object_filters"
8
+ spec.version = PandocObjectFilters::VERSION
9
+ spec.authors = ["Mike Virata-Stone"]
10
+ spec.email = ["mjstone@on-site.com"]
11
+
12
+ spec.summary = "A library for object based pandoc filters."
13
+ spec.description = "This is a small library for creating pandoc filters using Ruby objects. It is forked from the pandoc-filter gem."
14
+ spec.homepage = "https://github.com/smellsblue/pandoc_object_filters"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rubocop", "0.43.0"
28
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pandoc_object_filters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Virata-Stone
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.43.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.43.0
69
+ description: This is a small library for creating pandoc filters using Ruby objects.
70
+ It is forked from the pandoc-filter gem.
71
+ email:
72
+ - mjstone@on-site.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - examples/README.md
88
+ - examples/caps-sample.md
89
+ - examples/caps.rb
90
+ - examples/comments-sample.md
91
+ - examples/comments.rb
92
+ - examples/deflists-sample.md
93
+ - examples/deflists.rb
94
+ - examples/format-sample.md
95
+ - examples/format.rb
96
+ - examples/metavars-sample.md
97
+ - examples/metavars.rb
98
+ - lib/pandoc_object_filters.rb
99
+ - lib/pandoc_object_filters/element.rb
100
+ - lib/pandoc_object_filters/element/attr.rb
101
+ - lib/pandoc_object_filters/element/base.rb
102
+ - lib/pandoc_object_filters/element/base_element.rb
103
+ - lib/pandoc_object_filters/element/block.rb
104
+ - lib/pandoc_object_filters/element/document.rb
105
+ - lib/pandoc_object_filters/element/enum.rb
106
+ - lib/pandoc_object_filters/element/inline.rb
107
+ - lib/pandoc_object_filters/element/meta.rb
108
+ - lib/pandoc_object_filters/element/meta_value.rb
109
+ - lib/pandoc_object_filters/element/target.rb
110
+ - lib/pandoc_object_filters/filter.rb
111
+ - lib/pandoc_object_filters/version.rb
112
+ - lib/pandoc_object_filters/walker.rb
113
+ - pandoc_object_filters.gemspec
114
+ homepage: https://github.com/smellsblue/pandoc_object_filters
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.8
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: A library for object based pandoc filters.
138
+ test_files: []