fmedit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 73e63062f1bf7edd35be5c8118fd9a3c6cf2c9c10c792a0433384714ba0c25ca
4
+ data.tar.gz: 315cbdd463bcacbbba05461c39413d755f7e8eb810e1d562ded1f5cb9ff9df0f
5
+ SHA512:
6
+ metadata.gz: a441944a82b1d116c0ce3b9917bd44f2ed1c316eb58d481534debf649af86cdd8c5d34627877eee0ac0ceade88bc72a4010feca2bb4e21f876b2a2d783eb81e0
7
+ data.tar.gz: e40ad68e8df28317607ce4d3c8d237f660e95483b783d5dd3d1989ebc38210c9431b8bc50cfca80f28fcfb480a9433e0b103580933891a623586748b09c9edd3
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-07-03
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jamie@ideasasylum.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in fmedit.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "standard", "~> 1.3"
data/Gemfile.lock ADDED
@@ -0,0 +1,62 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fmedit (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ json (2.6.3)
11
+ language_server-protocol (3.17.0.3)
12
+ lint_roller (1.0.0)
13
+ minitest (5.18.1)
14
+ parallel (1.23.0)
15
+ parser (3.2.2.3)
16
+ ast (~> 2.4.1)
17
+ racc
18
+ racc (1.7.1)
19
+ rainbow (3.1.1)
20
+ rake (13.0.6)
21
+ regexp_parser (2.8.1)
22
+ rexml (3.2.5)
23
+ rubocop (1.52.1)
24
+ json (~> 2.3)
25
+ parallel (~> 1.10)
26
+ parser (>= 3.2.2.3)
27
+ rainbow (>= 2.2.2, < 4.0)
28
+ regexp_parser (>= 1.8, < 3.0)
29
+ rexml (>= 3.2.5, < 4.0)
30
+ rubocop-ast (>= 1.28.0, < 2.0)
31
+ ruby-progressbar (~> 1.7)
32
+ unicode-display_width (>= 2.4.0, < 3.0)
33
+ rubocop-ast (1.29.0)
34
+ parser (>= 3.2.1.0)
35
+ rubocop-performance (1.18.0)
36
+ rubocop (>= 1.7.0, < 2.0)
37
+ rubocop-ast (>= 0.4.0)
38
+ ruby-progressbar (1.13.0)
39
+ standard (1.29.0)
40
+ language_server-protocol (~> 3.17.0.2)
41
+ lint_roller (~> 1.0)
42
+ rubocop (~> 1.52.0)
43
+ standard-custom (~> 1.0.0)
44
+ standard-performance (~> 1.1.0)
45
+ standard-custom (1.0.1)
46
+ lint_roller (~> 1.0)
47
+ standard-performance (1.1.0)
48
+ lint_roller (~> 1.0)
49
+ rubocop-performance (~> 1.18.0)
50
+ unicode-display_width (2.4.2)
51
+
52
+ PLATFORMS
53
+ x86_64-darwin-20
54
+
55
+ DEPENDENCIES
56
+ fmedit!
57
+ minitest (~> 5.0)
58
+ rake (~> 13.0)
59
+ standard (~> 1.3)
60
+
61
+ BUNDLED WITH
62
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Jamie Lawrence
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # Fmedit
2
+
3
+ A small library for manipulating the frontmatter in markdown files.
4
+
5
+ It assumes that the frontmatter is deliniated by `---` and in YAML format.
6
+
7
+ Fair warning: it worked when I used it but tests are… non existant. I assume you will be using it on version controlled files that can be easily compared and restored.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add fmedit
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install fmedit
18
+
19
+ ## Usage
20
+
21
+ There's no CLI (yet?) but this provides a little utility library for writing scripts to manipulate the frontmatter in markdown files.
22
+
23
+ For example, to add the "published" attribute to all your posts and save them:
24
+
25
+ ```ruby
26
+ require "fmedit"
27
+
28
+ Fmedit::Files.new("src/_posts/*") do |file|
29
+ file.edit! published, true
30
+ file.save!
31
+ end
32
+ ```
33
+
34
+ To unpublish all hidden posts, and remove the hidden attribute:
35
+
36
+ ```ruby
37
+ require "fmedit"
38
+
39
+ Fmedit::Files.new("src/_posts/*") do |file|
40
+ file.add "published", false if f.get("hidden")
41
+ file.remove "hidden"
42
+ end
43
+ ```
44
+
45
+ `Fmedit::Files.new` takes a directory wildcard as a argument, e.g. `"src/_posts/*"`. It requires a block to be passed, to which an instance of Fmedit::Editor will be passed.
46
+
47
+ The following operations can be performed:
48
+
49
+ `add`: Add a key/value pair only if it doesn't already exist
50
+
51
+ `add!`: Add a key/value pair even if one exists (overwrites it)
52
+
53
+ `edit`: Update a key if the value matches a regex. Replaces using regex groups. This is the most complex operation and allows you to use regular expressions to match values, then use those groups for the replacement. Uses String#gsub under the hood.
54
+
55
+ ```
56
+ file.edit "image", /^((?!images\/.+|\/|http).*)$/, "/images/\\1"
57
+ file.edit "image", /^(images\/.+)$/, "\/\\1"
58
+ ```
59
+
60
+ `remove`: Remove a key, if it exists
61
+
62
+ `get`: Get the value of a key
63
+
64
+ `print`: Print the contents of the frontmatter
65
+
66
+ `save!`: Save the file
67
+
68
+ ## Development
69
+
70
+ 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.
71
+
72
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ideasasylum/fmedit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ideasasylum/fmedit/blob/master/CODE_OF_CONDUCT.md).
77
+
78
+ ## License
79
+
80
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
81
+
82
+ ## Code of Conduct
83
+
84
+ Everyone interacting in the Fmedit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ideasasylum/fmedit/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fmedit
4
+ VERSION = "0.1.0"
5
+ end
data/lib/fmedit.rb ADDED
@@ -0,0 +1,116 @@
1
+ require "yaml"
2
+ require_relative "fmedit/version"
3
+
4
+ module Fmedit
5
+ class Files
6
+ attr_reader :markdown_files
7
+ def initialize dir
8
+ @markdown_files = File.join(dir)
9
+ end
10
+
11
+ def each
12
+ Dir.glob(markdown_files).each do |filename|
13
+ editor = Editor.new filename
14
+ yield editor
15
+ end
16
+
17
+ # f.add "hidden", true if f.get("published").nil?
18
+ # f.add "published", true
19
+
20
+ # if f.get("hidden") == true && f.get("published") == true
21
+ # f.add "exclude_from_pagination", true
22
+ # end
23
+ # f.remove "hidden"
24
+ # f.print!
25
+ # f.edit "image", /^((?!images\/.+|\/|http).*)$/, "/images/\\1"
26
+ # f.edit "image", /^(images\/.+)$/, "\/\\1"
27
+ # f.print!
28
+ end
29
+ end
30
+
31
+ class Editor
32
+ attr_reader :filename, :frontmatter, :contents
33
+
34
+ def initialize filename
35
+ @filename = filename
36
+ @frontmatter = {}
37
+ @contents = ""
38
+ parse
39
+ end
40
+
41
+ def parse
42
+ File.open(filename, "r") do |file|
43
+ in_frontmatter = false
44
+ read_frontmatter = false
45
+ frontmatter_string = ""
46
+
47
+ file.each_line do |line|
48
+ if line.start_with? "---"
49
+ # start reading yaml strings
50
+ if !in_frontmatter && !read_frontmatter
51
+ in_frontmatter = true
52
+ elsif in_frontmatter
53
+ in_frontmatter = false
54
+ read_frontmatter = true
55
+ end
56
+ elsif in_frontmatter
57
+ frontmatter_string << line
58
+ else
59
+ @contents << line
60
+ end
61
+ end
62
+
63
+ @frontmatter = YAML.safe_load(frontmatter_string)
64
+ end
65
+ end
66
+
67
+ def edit key, regex, replacement
68
+ return unless frontmatter[key]
69
+
70
+ @frontmatter[key] = @frontmatter[key].gsub(regex, replacement)
71
+ end
72
+
73
+ def add key, value
74
+ return if @frontmatter.has_key? key
75
+
76
+ add! key, value
77
+ end
78
+
79
+ # Add the key/value even if it exists
80
+ def add! key, value
81
+ @frontmatter[key] = value
82
+ end
83
+
84
+ # Remove the key/value
85
+ def remove key
86
+ @frontmatter.delete(key)
87
+ end
88
+
89
+ # Get the value of the key
90
+ def get key
91
+ @frontmatter[key]
92
+ end
93
+
94
+ # Print the frontmatter and contents to stdout
95
+ def print frontmatter_only: true
96
+ puts frontmatter.to_yaml
97
+ puts "---\n"
98
+ if !frontmatter_only
99
+ contents.each_line do |line|
100
+ puts line
101
+ end
102
+ end
103
+ end
104
+
105
+ # Save the frontmatter and contents back to the file
106
+ def save!
107
+ File.open(filename, "w") do |file|
108
+ file.puts frontmatter.to_yaml
109
+ file.puts "---\n"
110
+ contents.each_line do |line|
111
+ file.puts line
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
data/sig/fmedit.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Fmedit
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fmedit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jamie Lawrence
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Utilities for editing frontmatter in markdown files
14
+ email:
15
+ - jamie@ideasasylum.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".standard.yml"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/fmedit.rb
29
+ - lib/fmedit/version.rb
30
+ - sig/fmedit.rbs
31
+ homepage: https://github.com/ideasasylum/fmedit
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/ideasasylum/fmedit
36
+ source_code_uri: https://github.com/ideasasylum/fmedit
37
+ changelog_uri: https://github.com/ideasasylum/fmedit/CHANGELOG.md
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.3.7
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Edit frontmatter in markdown files
57
+ test_files: []