From 4cb0ce21d5065584e2d91b5bde7715fd21242cc0 Mon Sep 17 00:00:00 2001 From: Dmitry Agapov Date: Sat, 11 Feb 2023 19:30:38 +0800 Subject: [PATCH 1/3] Added description about Sablon --- lib/curated/renderer.rb | 1 + lib/md_formating.rb | 7 +++++++ lib/ruby/sablon.rb | 28 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 lib/md_formating.rb create mode 100644 lib/ruby/sablon.rb diff --git a/lib/curated/renderer.rb b/lib/curated/renderer.rb index fa203ea..2625f05 100644 --- a/lib/curated/renderer.rb +++ b/lib/curated/renderer.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'erb' +require_relative 'md_formating' class Curated::Renderer def call diff --git a/lib/md_formating.rb b/lib/md_formating.rb new file mode 100644 index 0000000..d510064 --- /dev/null +++ b/lib/md_formating.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module MdFormating + def ruby_example_code(text) + (+'```ruby') << "\n#{text}\n```" + end +end diff --git a/lib/ruby/sablon.rb b/lib/ruby/sablon.rb new file mode 100644 index 0000000..b058b2c --- /dev/null +++ b/lib/ruby/sablon.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +class Sablon < Curated::RubyGem + extend MdFormating + + package 'sablon' + homepage 'https://github.com/senny/sablon' + category Category::Other + + text = + '# First prepare docx file and add `MergeField` inside. For example the name of this MergeField is some_variable' \ + "\n\nrequire 'sablon'\n\n template = Sablon.template(File.expand_path('/Users/bs/Documents/example_merge_field.docx'))" \ + "\ncontext = { some_variable: 'This text will be added instead of `some_variable` in resulted docx file' }" \ + "\n\ntemplate.render_to_file File.expand_path('~/Users/bs/Documents/output.docx'), context" + + pros "We've been usi Sablon in some projects for inserting content in docx templates." + pros "It is easy to use this gem:\n #{ruby_example_code(text)}" + pros "You can add to your documents simple values(which are the results of calculations in your methods), + formatted html blocks containing everything you need (tables, unsorted lists, sorted lists, + any text markup you need to add to the final docx file)" + cons "It does not support ODT format. And if you try to save docx file with MergeFields from LibreOffice your fields + will be saved as plain text, so you will not be able to paste the content into the document." + cons "If in your original template there is numbering (like 1.1, 1.2, 1.3, 1.4, 2.1, 2.2, 2,3, 2.4) + and for example you need in some cases to insert HTML block of text at number 1.3 and in some cases at number 2.3, + then be prepared to write code that will add numbering to HTML text block and writing logic in what case to insert + paragraph 1.3 and in what 2.3. Any addition of an HTML block to the document template will completely replace + the line in which MergeField is added" +end From 95120b180660082e25a6d88d2d6d1867f6506cf8 Mon Sep 17 00:00:00 2001 From: Dmitry Agapov Date: Mon, 13 Feb 2023 20:30:12 +0800 Subject: [PATCH 2/3] Fix after CR --- lib/curated/renderer.rb | 1 - lib/curated/utils.rb | 1 + lib/{ => curated/utils}/md_formating.rb | 3 ++- lib/ruby/sablon.rb | 6 ++---- 4 files changed, 5 insertions(+), 6 deletions(-) rename lib/{ => curated/utils}/md_formating.rb (80%) diff --git a/lib/curated/renderer.rb b/lib/curated/renderer.rb index 2625f05..fa203ea 100644 --- a/lib/curated/renderer.rb +++ b/lib/curated/renderer.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require 'erb' -require_relative 'md_formating' class Curated::Renderer def call diff --git a/lib/curated/utils.rb b/lib/curated/utils.rb index e1e3a0b..c3339e4 100644 --- a/lib/curated/utils.rb +++ b/lib/curated/utils.rb @@ -3,6 +3,7 @@ require_relative 'utils/github' require_relative 'utils/string' require_relative 'utils/npm' +require_relative 'utils/md_formating' module Utils end diff --git a/lib/md_formating.rb b/lib/curated/utils/md_formating.rb similarity index 80% rename from lib/md_formating.rb rename to lib/curated/utils/md_formating.rb index d510064..60a50ce 100644 --- a/lib/md_formating.rb +++ b/lib/curated/utils/md_formating.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true -module MdFormating +module Utils + extend self def ruby_example_code(text) (+'```ruby') << "\n#{text}\n```" end diff --git a/lib/ruby/sablon.rb b/lib/ruby/sablon.rb index b058b2c..5235f35 100644 --- a/lib/ruby/sablon.rb +++ b/lib/ruby/sablon.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true -class Sablon < Curated::RubyGem - extend MdFormating - +class Curated::Sablon < Curated::RubyGem package 'sablon' homepage 'https://github.com/senny/sablon' category Category::Other @@ -14,7 +12,7 @@ class Sablon < Curated::RubyGem "\n\ntemplate.render_to_file File.expand_path('~/Users/bs/Documents/output.docx'), context" pros "We've been usi Sablon in some projects for inserting content in docx templates." - pros "It is easy to use this gem:\n #{ruby_example_code(text)}" + pros "It is easy to use this gem:\n #{Utils.ruby_example_code(text)}" pros "You can add to your documents simple values(which are the results of calculations in your methods), formatted html blocks containing everything you need (tables, unsorted lists, sorted lists, any text markup you need to add to the final docx file)" From 5a2aab27aed72e0b2a4a9351f992f92084e63d16 Mon Sep 17 00:00:00 2001 From: Dmitry Agapov Date: Thu, 23 Feb 2023 03:07:57 +0800 Subject: [PATCH 3/3] Fix after CR. Improved method of displaying code blocks. Encapsulated helpers in anonymous class. --- lib/curated.rb | 8 ++++++++ lib/curated/utils/md_formating.rb | 7 +++++-- lib/ruby/sablon.rb | 19 ++++++++++++------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/curated.rb b/lib/curated.rb index 67a0ee5..2889951 100644 --- a/lib/curated.rb +++ b/lib/curated.rb @@ -95,6 +95,14 @@ def contributors path = "lib/#{collection}/#{Utils.underscore(Utils.demodulize(self))}.rb" Utils.github_contributors_for_file('jetrockets/curated', path) end + + private + + def helper_utils + @helper_utils ||= Class.new do + include Utils + end.new + end end attr_rw :package diff --git a/lib/curated/utils/md_formating.rb b/lib/curated/utils/md_formating.rb index 60a50ce..4eeefa8 100644 --- a/lib/curated/utils/md_formating.rb +++ b/lib/curated/utils/md_formating.rb @@ -2,7 +2,10 @@ module Utils extend self - def ruby_example_code(text) - (+'```ruby') << "\n#{text}\n```" + + def code_block(language, text = nil) + value = (+"```#{language}") << "\n#{text}```" + + block_given? ? yield(value) : value end end diff --git a/lib/ruby/sablon.rb b/lib/ruby/sablon.rb index 5235f35..748668d 100644 --- a/lib/ruby/sablon.rb +++ b/lib/ruby/sablon.rb @@ -5,14 +5,19 @@ class Curated::Sablon < Curated::RubyGem homepage 'https://github.com/senny/sablon' category Category::Other - text = - '# First prepare docx file and add `MergeField` inside. For example the name of this MergeField is some_variable' \ - "\n\nrequire 'sablon'\n\n template = Sablon.template(File.expand_path('/Users/bs/Documents/example_merge_field.docx'))" \ - "\ncontext = { some_variable: 'This text will be added instead of `some_variable` in resulted docx file' }" \ - "\n\ntemplate.render_to_file File.expand_path('~/Users/bs/Documents/output.docx'), context" - pros "We've been usi Sablon in some projects for inserting content in docx templates." - pros "It is easy to use this gem:\n #{Utils.ruby_example_code(text)}" + # rubocop:disable Layout/ClosingParenthesisIndentation + pros "It is easy to use this gem:\n #{helper_utils.code_block "ruby", <<~RUBY + # First prepare docx file and add `MergeField` inside. For example the name of this MergeField is some_variable + require 'sablon' + + template = Sablon.template(File.expand_path('/Users/bs/Documents/example_merge_field.docx')) + context = { some_variable: 'This text will be added instead of `some_variable` in resulted docx file' } + + template.render_to_file File.expand_path('~/Users/bs/Documents/output.docx'), context + RUBY + }" + # rubocop:enable Layout/ClosingParenthesisIndentation pros "You can add to your documents simple values(which are the results of calculations in your methods), formatted html blocks containing everything you need (tables, unsorted lists, sorted lists, any text markup you need to add to the final docx file)"