Skip to content

Commit

Permalink
Start preserving tabs when running filters (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
htdebeer committed Nov 26, 2023
1 parent 76a83f8 commit 76fcd2c
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

documentation/api-doc
.yardoc
.cache

/.bundle/
/.yardoc
Expand Down
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
AllCops:
TargetRubyVersion: 2.6.8
NewCops: enable
StyleGuideBaseURL: https://rubystyle.guide
Metrics/ClassLength:
Max: 150
Metrics/MethodLength:
Max: 25
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ gem "rake", "~> 13.0"
gem "yard", "~> 0.9"
gem "minitest", "~> 5.0"
gem "minitest-reporters", "~> 1.4"
gem "rubocop", "~> 1.56.4"
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "bundler/gem_tasks"
require "rake/testtask"
require 'rubocop/rake_task'
require "yard"

Rake::TestTask.new do |t|
Expand All @@ -22,6 +23,7 @@ task :generate_doc do
end

task :build do
Rake::Task[:rubocop].execute
Rake::Task["test"].execute
Rake::Task["yard"].execute
sh "gem build paru.gemspec; mv *.*.*.gem releases"
Expand Down
2 changes: 1 addition & 1 deletion lib/paru.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
#++
module Paru
# Paru's current version
VERSION = [1, 1, 2].freeze
VERSION = [1, 2, 0].freeze
end
4 changes: 2 additions & 2 deletions lib/paru/filter/cell.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#--
# Copyright 2020 Huub de Beer <Huub@heerdebeer.org>
# Copyright 2020, 2023 Huub de Beer <Huub@heerdebeer.org>
#
# This file is part of Paru
#
Expand All @@ -22,7 +22,7 @@

module Paru
module PandocFilter
# A Cell node represents a cell in a table's head, body, or foot
# A Cell node represents a cell in a table's head, body, or foot.
#
# @!attribute attr
# @return Attr
Expand Down
4 changes: 3 additions & 1 deletion lib/paru/filter/node.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#--
# Copyright 2015, 2016, 2017, 2020 Huub de Beer <Huub@heerdebeer.org>
# Copyright 2015, 2016, 2017, 2020, 2023 Huub de Beer <Huub@heerdebeer.org>
#
# This file is part of Paru
#
Expand All @@ -26,12 +26,14 @@ module PandocFilter
AST2MARKDOWN = Paru::Pandoc.new do
from "json"
to "markdown-smart"
preserve_tabs true
end

# A Paru::Pandoc converter from markdown to JSON
MARKDOWN2JSON = Paru::Pandoc.new do
from "markdown+smart"
to "json"
preserve_tabs true
end

# Every node in a Pandoc AST is mapped to Node. Filters are all about
Expand Down
106 changes: 73 additions & 33 deletions lib/paru/filter/table.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#--
# Copyright 2015, 2016, 2017, 2020 Huub de Beer <Huub@heerdebeer.org>
# Copyright 2015, 2016, 2017, 2020, 2023 Huub de Beer <Huub@heerdebeer.org>
#
# This file is part of Paru
#
Expand Down Expand Up @@ -131,46 +131,39 @@ def to_file(filename, config = {})
#
# @return [Table]
def self.from_array(data, config = {})
# With the updated Table definition, it has become complicated
# to construct a table manually. It has gotten easier to just
# construct a string containing a table in Pandoc's markdown and
# load that. I did remove setting alignments and column widths,
# though, because that is a bit of a hassle to get right.

markdown_table = ""
header = ""
footer = ""

if config.has_key? :headers and config[:headers] then
head_row = data.first
header += head_row.join(" \t") + "\n"
header += head_row.map {|s| s.gsub(/./, "-") + "-"}.join("\t") + "\n"
data = data.slice(1..-1)
end
table_attribute = create_attr

if config.has_key? :footers and config[:footers] then
foot_row = data.first
footer += foot_row.join(" \t") + "\n"
footer += foot_row.map {|s| s.gsub(/./, "-") + "-"}.join("\t") + "\n"
data = data.slice(0, -2)
caption = []
if config.has_key? :caption
caption = create_caption config[:caption]
end

data.each do |row|
markdown_table += row.join(" \t") + "\n"
end
col_spec = data[0].map {|c| ColSpec.new.to_ast }

markdown_table = header + markdown_table + footer
head = create_endrow []
if config.has_key? :headers and config[:headers]
head = create_endrow data.first
data = data[1..-1]
end

if config.has_key? :caption then
markdown_table += "\n"
markdown_table += ": #{config[:caption]}\n"
foot = create_endrow []
if config.has_key? :footers and config[:footers]
foot = create_endrow data.last
data = data[0...-1]
end

table = Block.new []
table.markdown = markdown_table
table = table.children.first
body = create_body data

table = [
table_attribute,
caption,
col_spec,
head,
body,
foot
]

table
Table.new table
end


Expand All @@ -188,6 +181,53 @@ def self.from_file(filename, config = {})

return self.from_array(data, config)
end

private

def self.create_caption(contents)
[
nil,
[Node.from_markdown(contents).to_ast]
]
end

def self.create_body(data)
[[
create_attr,
0,
[],
data.map {|r| create_row(r)}
]]
end

def self.create_endrow(data)
[
create_attr,
if data.empty? then [] else [create_row(data)] end
]
end

def self.create_row(data)
[
create_attr,
data.map {|c| create_cell(c)}
]
end

def self.create_cell(contents)
[
create_attr,
{"t" => "AlignDefault", "c" => nil},
1,
1,
[Node.from_markdown(contents).to_ast]
]
end

def self.create_attr()
["", [], []]
end

end
end
end
2 changes: 0 additions & 2 deletions lib/paru/filter/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ def ast_type()
@type
end



# Create an AST representation of this Node
#
# @return [Hash]
Expand Down
2 changes: 1 addition & 1 deletion paru.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Gem::Specification.new do |s|
s.name = 'paru'
s.license = 'GPL-3.0'
s.version = Paru::VERSION.join "."
s.date = '2023-06-30'
s.date = '2023-11-26'
s.authors = ['Huub de Beer']
s.email = 'Huub@heerdebeer.org'

Expand Down
8 changes: 4 additions & 4 deletions test/test_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
require_relative "../lib/paru/filter/definition_list.rb"
require_relative "../lib/paru/filter/table.rb"

class FilterTest < MiniTest::Test
class FilterTest < Minitest::Test

def setup
end
Expand All @@ -27,14 +27,14 @@ def filter_string(string, &filter_specification)
to "markdown"
standalone
end

json = pandoc2json << string

input = StringIO.new(json)
output = StringIO.new

Paru::Filter.new(input, output).filter(&filter_specification)

result = json2pandoc << output.string
return result
end
Expand Down Expand Up @@ -587,6 +587,7 @@ def test_table_convenience()

input_data = [["No", "String"], ["1", "Hello"], ["2", "Hi"], ["3", "Goodbye"]]
input = "replace this"

output = filter_string(input) do
with "Para" do |p|
table = Paru::PandocFilter::Table.from_array input_data, headers: true, caption: "A list of greetings."
Expand All @@ -595,7 +596,6 @@ def test_table_convenience()
end

assert_match(output, File.read("test/pandoc_input/table.md"))


# To file
csv = File.read("test/pandoc_output/table.csv")
Expand Down
2 changes: 1 addition & 1 deletion test/test_paru.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative "../lib/paru/pandoc.rb"
require_relative "../lib/paru/error.rb"

class ParuTest < MiniTest::Test
class ParuTest < Minitest::Test

def setup
end
Expand Down

0 comments on commit 76fcd2c

Please sign in to comment.