mediawiki-replaceable-content 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +34 -0
- data/.gitignore +10 -0
- data/.rubocop.yml +37 -0
- data/.travis.yml +10 -0
- data/CHANGELOG.md +24 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +166 -0
- data/Rakefile +15 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/exe/fibonacci_example +47 -0
- data/lib/mediawiki/client.rb +27 -0
- data/lib/mediawiki/page.rb +121 -0
- data/mediawiki-page-replaceable_content.gemspec +41 -0
- metadata +146 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6cba1d4d392f832817deceae7de443e872a0eeafa4c6321b1e5eab31baae996c
|
4
|
+
data.tar.gz: ce45f42317be4eb3ae5dca06d900e427d5b66fa9f42da8b9ac077495eaddcd01
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 98f954935950afbd03bfed2bee7e7d0f5805d86147a6661c6361b801087ab983327ff9525ac72b540f0bb1ab1e201d5f8d082e690433ebd729365e397e44f6bb
|
7
|
+
data.tar.gz: 2f4a1089e4831f9f3a0bf355f1419982a62270bac5f2bbe3da81f5ac1b4b3ffb28fdf6e43bb391f7d59357878ace77c4fe4a16170638808f31ef22520e3ce7d0
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Ruby
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ master ]
|
13
|
+
pull_request:
|
14
|
+
branches: [ master ]
|
15
|
+
|
16
|
+
jobs:
|
17
|
+
test:
|
18
|
+
|
19
|
+
runs-on: ubuntu-latest
|
20
|
+
|
21
|
+
steps:
|
22
|
+
- uses: actions/checkout@v2
|
23
|
+
- name: Set up Ruby
|
24
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
25
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
26
|
+
# uses: ruby/setup-ruby@v1
|
27
|
+
uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
|
28
|
+
with:
|
29
|
+
ruby-version: 2.5
|
30
|
+
bundler: 1
|
31
|
+
- name: Install dependencies
|
32
|
+
run: bundle install
|
33
|
+
- name: Run tests
|
34
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'Vagrantfile'
|
4
|
+
- 'vendor/**/*'
|
5
|
+
- 'exe/**/*'
|
6
|
+
TargetRubyVersion: 2.5
|
7
|
+
NewCops: enable
|
8
|
+
|
9
|
+
Metrics/AbcSize:
|
10
|
+
Exclude:
|
11
|
+
- 'test/test_helper.rb'
|
12
|
+
|
13
|
+
Metrics/BlockLength:
|
14
|
+
Exclude:
|
15
|
+
- '**/*.gemspec'
|
16
|
+
- 'test/**/*.rb'
|
17
|
+
|
18
|
+
Layout/LineLength:
|
19
|
+
Max: 150
|
20
|
+
Exclude:
|
21
|
+
- 'test/**/*.rb'
|
22
|
+
|
23
|
+
Layout/HashAlignment:
|
24
|
+
EnforcedHashRocketStyle: table
|
25
|
+
EnforcedColonStyle: table
|
26
|
+
|
27
|
+
Style/CollectionMethods:
|
28
|
+
Enabled: true
|
29
|
+
|
30
|
+
Style/Documentation:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Style/FormatStringToken:
|
34
|
+
Enabled: false
|
35
|
+
|
36
|
+
Style/HashSyntax:
|
37
|
+
EnforcedStyle: ruby19_no_mixed_keys
|
data/.travis.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
cache: bundler
|
4
|
+
rvm:
|
5
|
+
- 2.5
|
6
|
+
- 2.6
|
7
|
+
before_install: gem install bundler -v 1.15.3
|
8
|
+
script:
|
9
|
+
- bundle exec rake
|
10
|
+
- bash <(curl -fsSL https://github.com/everypolitician/ensure-regression-tests/raw/master/ensure-regression-tests)
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
6
|
+
and this project adheres to [Semantic Versioning](http://semver.org/).
|
7
|
+
|
8
|
+
# [0.2.1] - 2020-00-01
|
9
|
+
|
10
|
+
## Fixes
|
11
|
+
|
12
|
+
Replacement page content can now contain percentage signs.
|
13
|
+
|
14
|
+
# [0.2.0] - 2020-00-01
|
15
|
+
|
16
|
+
The minimum allowed ruby verson is now 2.5
|
17
|
+
|
18
|
+
# [0.1.4] - 2018-11-27
|
19
|
+
|
20
|
+
Better handling for a template not being found.
|
21
|
+
|
22
|
+
# [0.1.3] - 2017-11-03
|
23
|
+
|
24
|
+
Support numbered and anonymous parameters
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Mark Longair
|
4
|
+
Copyright (c) 2020 Tony Bowden
|
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,166 @@
|
|
1
|
+
# MediaWiki::Page::ReplaceableContent
|
2
|
+
|
3
|
+
This gem provides a class,
|
4
|
+
`MediaWiki::Page::ReplaceableContent`, to help you
|
5
|
+
programmatically rewrite MediaWiki pages based on the parameters
|
6
|
+
of a template tag in that page. (This is a model used by the
|
7
|
+
[Listeria](https://tools.wmflabs.org/listeria/) bot, for
|
8
|
+
example.)
|
9
|
+
|
10
|
+
See the "Usage" section below for an example.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'mediawiki-replaceable-content'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install mediawiki-replaceable-content
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
To use the `ReplaceableContent` class, you must first create a
|
31
|
+
`Client` object, which specifies the MediaWiki site on which you
|
32
|
+
want to rewrite a page, and the username and password for
|
33
|
+
authentication.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'mediawiki/client'
|
37
|
+
|
38
|
+
client = MediaWiki::Client.new(
|
39
|
+
site: 'www.wikidata.org',
|
40
|
+
username: ENV['WIKI_USERNAME'],
|
41
|
+
password: ENV['WIKI_PASSWORD']
|
42
|
+
)
|
43
|
+
```
|
44
|
+
|
45
|
+
Then you can use that to create a `ReplaceableContent` class,
|
46
|
+
which specifies a particular page and the template on that page
|
47
|
+
that your content will be placed directly after:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'mediawiki/page'
|
51
|
+
|
52
|
+
section = MediaWiki::Page::ReplaceableContent.new(
|
53
|
+
client: client,
|
54
|
+
title: 'User:Mhl20/Fibonnacci test',
|
55
|
+
template: 'Fibonacci'
|
56
|
+
)
|
57
|
+
```
|
58
|
+
|
59
|
+
For example, suppose that page at
|
60
|
+
https://www.wikidata.org/wiki/User:Mhl20/Fibonnacci_test had the
|
61
|
+
following content:
|
62
|
+
|
63
|
+
```
|
64
|
+
Here are some Fibonacci numbers:
|
65
|
+
|
66
|
+
{{Fibonacci
|
67
|
+
|max_fib=10
|
68
|
+
}}
|
69
|
+
|
70
|
+
You can find more about them here:
|
71
|
+
https://en.wikipedia.org/wiki/Fibonacci_number
|
72
|
+
```
|
73
|
+
|
74
|
+
Then you could rewrite it, using the `ReplaceableContent` object
|
75
|
+
created above, with:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
def fibonacci_numbers(limit)
|
79
|
+
numbers = []
|
80
|
+
i, j = 0, 1
|
81
|
+
while i <= limit
|
82
|
+
numbers << i
|
83
|
+
i, j = j, i + j
|
84
|
+
end
|
85
|
+
numbers
|
86
|
+
end
|
87
|
+
|
88
|
+
def wikitext_fibonacci_rows(limit)
|
89
|
+
fibs = fibonacci_numbers(limit)
|
90
|
+
"|-\n" + 0.upto(fibs.length - 1).map { |f| "| ''F''<sub>#{f}</sub>\n" }.join('') +
|
91
|
+
"|-\n" + fibs.map { |n| "| #{n}\n" }.join('')
|
92
|
+
end
|
93
|
+
|
94
|
+
def wikitext_fibonacci(limit)
|
95
|
+
"=== Some Fibonacci numbers ===\n:{| class=\"wikitable\"\n" +
|
96
|
+
wikitext_fibonacci_rows(limit) +
|
97
|
+
'|}'
|
98
|
+
end
|
99
|
+
|
100
|
+
limit = Integer(section.params[:max_fib])
|
101
|
+
section.replace_output(
|
102
|
+
wikitext_fibonacci(limit),
|
103
|
+
"Rewrote with numbers up to #{limit}"
|
104
|
+
)
|
105
|
+
```
|
106
|
+
|
107
|
+
The result of running that would be that the page had the
|
108
|
+
following wikitext:
|
109
|
+
|
110
|
+
```
|
111
|
+
Here are some Fibonacci numbers:
|
112
|
+
|
113
|
+
{{Fibonacci
|
114
|
+
|max_fib=10
|
115
|
+
}}
|
116
|
+
=== Some Fibonacci numbers ===
|
117
|
+
:{| class="wikitable"
|
118
|
+
|-
|
119
|
+
| ''F''<sub>0</sub>
|
120
|
+
| ''F''<sub>1</sub>
|
121
|
+
| ''F''<sub>2</sub>
|
122
|
+
| ''F''<sub>3</sub>
|
123
|
+
| ''F''<sub>4</sub>
|
124
|
+
| ''F''<sub>5</sub>
|
125
|
+
| ''F''<sub>6</sub>
|
126
|
+
|-
|
127
|
+
| 0
|
128
|
+
| 1
|
129
|
+
| 1
|
130
|
+
| 2
|
131
|
+
| 3
|
132
|
+
| 5
|
133
|
+
| 8
|
134
|
+
|}
|
135
|
+
<!-- OUTPUT END Rewrote with numbers up to 10 -->
|
136
|
+
|
137
|
+
You can find more about them here:
|
138
|
+
https://en.wikipedia.org/wiki/Fibonacci_number
|
139
|
+
```
|
140
|
+
|
141
|
+
On subsequent uses, only the text between the template tag and
|
142
|
+
the `<!-- OUTPUT END ... --->` comment will be rewritten.
|
143
|
+
|
144
|
+
If you need to rely on the previous content, you can get that
|
145
|
+
with `section.existing_content`.
|
146
|
+
|
147
|
+
## Development
|
148
|
+
|
149
|
+
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.
|
150
|
+
|
151
|
+
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).
|
152
|
+
|
153
|
+
## Contributing
|
154
|
+
|
155
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tmtmtmtm/mediawiki-replaceable-content
|
156
|
+
|
157
|
+
## License
|
158
|
+
|
159
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
160
|
+
|
161
|
+
## History
|
162
|
+
|
163
|
+
This was originally developed as part of a [Wikimedia Foundation grant-funded
|
164
|
+
project](https://meta.wikimedia.org/wiki/Grants:Project/mySociety/EveryPolitician).
|
165
|
+
|
166
|
+
This version is now maintained independently by Tony Bowden.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
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 'rubocop/rake_task'
|
13
|
+
RuboCop::RakeTask.new
|
14
|
+
|
15
|
+
task default: %i[test rubocop]
|
data/bin/console
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'mediawiki/client'
|
6
|
+
require 'mediawiki/page'
|
7
|
+
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
10
|
+
|
11
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
12
|
+
# require "pry"
|
13
|
+
# Pry.start
|
14
|
+
|
15
|
+
require 'irb'
|
16
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'mediawiki/client'
|
5
|
+
require 'mediawiki/page'
|
6
|
+
|
7
|
+
client = MediaWiki::Client.new(
|
8
|
+
site: 'www.wikidata.org',
|
9
|
+
username: ENV['WIKI_USERNAME'],
|
10
|
+
password: ENV['WIKI_PASSWORD']
|
11
|
+
)
|
12
|
+
|
13
|
+
section = MediaWiki::Page::ReplaceableContent.new(
|
14
|
+
client: client,
|
15
|
+
title: 'User:Mhl20/Fibonnacci test',
|
16
|
+
template: 'Fibonacci'
|
17
|
+
)
|
18
|
+
|
19
|
+
def fibonacci_numbers(limit)
|
20
|
+
numbers = []
|
21
|
+
i = 0
|
22
|
+
j = 1
|
23
|
+
while i <= limit
|
24
|
+
numbers << i
|
25
|
+
i = j
|
26
|
+
j = i + j
|
27
|
+
end
|
28
|
+
numbers
|
29
|
+
end
|
30
|
+
|
31
|
+
def wikitext_fibonacci_rows(limit)
|
32
|
+
fibs = fibonacci_numbers(limit)
|
33
|
+
"|-\n" + 0.upto(fibs.length - 1).map { |f| "| ''F''<sub>#{f}</sub>\n" }.join('') +
|
34
|
+
"|-\n" + fibs.map { |n| "| #{n}\n" }.join('')
|
35
|
+
end
|
36
|
+
|
37
|
+
def wikitext_fibonacci(limit)
|
38
|
+
"=== Some Fibonacci numbers ===\n:{| class=\"wikitable\"\n" +
|
39
|
+
wikitext_fibonacci_rows(limit) +
|
40
|
+
'|}'
|
41
|
+
end
|
42
|
+
|
43
|
+
limit = Integer(section.params[:max_fib])
|
44
|
+
section.replace_output(
|
45
|
+
wikitext_fibonacci(limit),
|
46
|
+
"Rewrote with numbers up to #{limit}"
|
47
|
+
)
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mediawiki_api'
|
4
|
+
|
5
|
+
module MediaWiki
|
6
|
+
class Client
|
7
|
+
def initialize(site:, username:, password:)
|
8
|
+
@site = site
|
9
|
+
@username = username
|
10
|
+
@password = password
|
11
|
+
end
|
12
|
+
|
13
|
+
extend Forwardable
|
14
|
+
def_delegators :wrapped_client, :edit, :get_wikitext
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_accessor :site, :username, :password
|
19
|
+
|
20
|
+
def wrapped_client
|
21
|
+
@wrapped_client ||= MediawikiApi::Client.new("https://#{site}/w/api.php").tap do |c|
|
22
|
+
result = c.log_in(username, password)
|
23
|
+
raise "MediawikiApi::Client#log_in failed: #{result}" if result['result'] != 'Success'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mediawiki_api'
|
4
|
+
|
5
|
+
module MediaWiki
|
6
|
+
module Page
|
7
|
+
class ReplaceableContent
|
8
|
+
NAMED_TEMPLATE_PARAM_RE = /(.*?)=(.*)/m.freeze
|
9
|
+
|
10
|
+
TemplateNotFoundError = Class.new(StandardError)
|
11
|
+
|
12
|
+
def initialize(client:, title:, template:)
|
13
|
+
@client = client
|
14
|
+
@title = title
|
15
|
+
@template = template
|
16
|
+
end
|
17
|
+
|
18
|
+
def existing_content
|
19
|
+
page_parts[:to_replace]
|
20
|
+
end
|
21
|
+
|
22
|
+
def reassemble_page(new_content, run_comment = '')
|
23
|
+
format("%<before>s{{%<template_name>s%<parameters>s}}\n%<new_content>s\n<!-- OUTPUT END %<run_comment>s -->%<keep>s",
|
24
|
+
page_parts.merge(new_content: new_content, run_comment: run_comment))
|
25
|
+
end
|
26
|
+
|
27
|
+
def replace_output(new_content, run_comment = '')
|
28
|
+
new_wikitext = reassemble_page(new_content, run_comment)
|
29
|
+
client.edit(title: title, text: new_wikitext)
|
30
|
+
end
|
31
|
+
|
32
|
+
def param_key_value_array(key, value)
|
33
|
+
[
|
34
|
+
if key.strip =~ /^\d+$/
|
35
|
+
Integer(Regexp.last_match[0])
|
36
|
+
else
|
37
|
+
key.strip.to_sym
|
38
|
+
end,
|
39
|
+
value
|
40
|
+
]
|
41
|
+
end
|
42
|
+
|
43
|
+
def params
|
44
|
+
# Returns the named parameters from the template tag as a Hash
|
45
|
+
# where the keys are symbolized versions of the parameter
|
46
|
+
# names.
|
47
|
+
# FIXME: untemplate these using the MediaWiki API before
|
48
|
+
# returning them.
|
49
|
+
all_parameters.map do |p|
|
50
|
+
m = NAMED_TEMPLATE_PARAM_RE.match(p)
|
51
|
+
param_key_value_array(*m.captures) if m
|
52
|
+
end.compact.to_h
|
53
|
+
end
|
54
|
+
|
55
|
+
def anonymous_params
|
56
|
+
# FIXME: untemplate these using the MediaWiki API before
|
57
|
+
# returning them.
|
58
|
+
all_parameters.map { |p| NAMED_TEMPLATE_PARAM_RE =~ p ? nil : p }.compact
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
attr_accessor :client, :title, :template
|
64
|
+
|
65
|
+
def wikitext
|
66
|
+
@wikitext ||= client.get_wikitext(title).body
|
67
|
+
end
|
68
|
+
|
69
|
+
def template_re
|
70
|
+
# FIXME: there are obviously better ways of doing this parsing
|
71
|
+
# than with a regular expression. e.g. there is an EBNF
|
72
|
+
# version of the MediaWiki grammar which we could use to
|
73
|
+
# generate a proper parser.
|
74
|
+
/
|
75
|
+
^(?<before>.*?)
|
76
|
+
\{\{
|
77
|
+
(?<template_name>#{Regexp.quote(template)})
|
78
|
+
(?<parameters>.*?)
|
79
|
+
\}\}
|
80
|
+
(?<after>.*)$
|
81
|
+
/xm
|
82
|
+
end
|
83
|
+
|
84
|
+
def page_parts
|
85
|
+
return @page_parts if @page_parts
|
86
|
+
|
87
|
+
m = template_re.match(wikitext)
|
88
|
+
raise TemplateNotFoundError, "The template '#{template}' was not found in '#{title}'" unless m
|
89
|
+
|
90
|
+
parts = matchdata_to_h(m)
|
91
|
+
@page_parts = parts.merge(split_after(parts[:after]))
|
92
|
+
end
|
93
|
+
|
94
|
+
def split_after(after)
|
95
|
+
# The part of the page after the template may or may not have
|
96
|
+
# the special HTML comment that marks the end of the previous
|
97
|
+
# output; if it's not there we insert the new content right
|
98
|
+
# after the template tag.
|
99
|
+
if after =~ /^(?<to_replace>.*?)\n<!-- OUTPUT END (?:.*?)-->(?<keep>.*)/m
|
100
|
+
matchdata_to_h(Regexp.last_match)
|
101
|
+
else
|
102
|
+
{ to_replace: '', keep: after }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def all_parameters
|
107
|
+
# This returns all parameters, including anonymous, numbered and
|
108
|
+
# named parameters. (Though we only handle named parameters at
|
109
|
+
# the moment.)
|
110
|
+
page_parts[:parameters].split('|').select do |s|
|
111
|
+
s.strip!
|
112
|
+
s.empty? ? nil : s
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def matchdata_to_h(matchdata)
|
117
|
+
matchdata.names.map(&:to_sym).zip(matchdata.captures).to_h
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.required_ruby_version = '>= 2.5.0'
|
8
|
+
|
9
|
+
spec.name = 'mediawiki-replaceable-content'
|
10
|
+
spec.version = '0.2.1'
|
11
|
+
spec.authors = ['Tony Bowden', 'Mark Longair']
|
12
|
+
spec.email = ['tony@tmtm.com']
|
13
|
+
spec.licenses = ['MIT']
|
14
|
+
|
15
|
+
spec.summary = 'Rewrite content after a template tag based on the template parameters'
|
16
|
+
spec.homepage = 'https://github.com/tmtm/mediawiki-replaceable-content'
|
17
|
+
|
18
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
19
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
20
|
+
if spec.respond_to?(:metadata)
|
21
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
22
|
+
else
|
23
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
24
|
+
'public gem pushes.'
|
25
|
+
end
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(test|spec|features)/})
|
29
|
+
end
|
30
|
+
spec.bindir = 'exe'
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
|
34
|
+
spec.add_runtime_dependency 'mediawiki_api', '~> 0.7'
|
35
|
+
|
36
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
37
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
38
|
+
spec.add_development_dependency 'pry', '~> 0.10'
|
39
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
40
|
+
spec.add_development_dependency 'rubocop'
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mediawiki-replaceable-content
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tony Bowden
|
8
|
+
- Mark Longair
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2020-09-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mediawiki_api
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0.7'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0.7'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.15'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.15'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: minitest
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '5.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '5.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: pry
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "~>"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0.10'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0.10'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rake
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '10.0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '10.0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rubocop
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
description:
|
99
|
+
email:
|
100
|
+
- tony@tmtm.com
|
101
|
+
executables:
|
102
|
+
- fibonacci_example
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- ".github/workflows/ruby.yml"
|
107
|
+
- ".gitignore"
|
108
|
+
- ".rubocop.yml"
|
109
|
+
- ".travis.yml"
|
110
|
+
- CHANGELOG.md
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- bin/console
|
116
|
+
- bin/setup
|
117
|
+
- exe/fibonacci_example
|
118
|
+
- lib/mediawiki/client.rb
|
119
|
+
- lib/mediawiki/page.rb
|
120
|
+
- mediawiki-page-replaceable_content.gemspec
|
121
|
+
homepage: https://github.com/tmtm/mediawiki-replaceable-content
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
metadata:
|
125
|
+
allowed_push_host: https://rubygems.org
|
126
|
+
post_install_message:
|
127
|
+
rdoc_options: []
|
128
|
+
require_paths:
|
129
|
+
- lib
|
130
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: 2.5.0
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
requirements: []
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 2.7.6.2
|
143
|
+
signing_key:
|
144
|
+
specification_version: 4
|
145
|
+
summary: Rewrite content after a template tag based on the template parameters
|
146
|
+
test_files: []
|