From a4242dcac99a8c8991d423ff8149ad40ad4d72c0 Mon Sep 17 00:00:00 2001 From: Yessiest Date: Wed, 28 Feb 2024 03:06:46 +0400 Subject: [PATCH] Restructured the repo --- mdpp.rb => bin/mdpp | 66 +++++++++++++--- classes | 13 --- document.rb => lib/rbmark.rb | 0 rbmark.gemspec | 21 +++++ test.md | 149 ----------------------------------- 5 files changed, 77 insertions(+), 172 deletions(-) rename mdpp.rb => bin/mdpp (88%) mode change 100644 => 100755 delete mode 100644 classes rename document.rb => lib/rbmark.rb (100%) create mode 100644 rbmark.gemspec delete mode 100644 test.md diff --git a/mdpp.rb b/bin/mdpp old mode 100644 new mode 100755 similarity index 88% rename from mdpp.rb rename to bin/mdpp index ab1dd0c..8d1e077 --- a/mdpp.rb +++ b/bin/mdpp @@ -1,7 +1,8 @@ #!/usr/bin/ruby # frozen_string_literal: true -require_relative 'document' +require 'optparse' +require 'rbmark' require 'io/console' require 'io/console/size' @@ -191,7 +192,6 @@ module MDPP # Long bracket for code blocks def longbracket(text, properties) - puts properties.inspect textlines = text.lines textlines = textlines.map do |line| "│ #{line}" @@ -203,6 +203,8 @@ module MDPP # Add text to bibliography def bibliography(text, properties) + return "#{text}[#{properties['element'][:link]}]" if @options['nb'] + @bibliography.append([text, properties['element'][:link]]) "#{text}[#{@bibliography.length + 1}]" end @@ -318,10 +320,9 @@ module MDPP # @param options [Hash] def initialize(input, options) @doc = RBMark::DOM::Document.parse(input) - @color_mode = options.fetch("color", true) - @ansi_mode = options.fetch("ansi", true) @style = ::MDPP::DEFAULT_STYLE.dup @bibliography = [] + @options = options return unless options['style'] @style = @style.map do |k, v| @@ -334,7 +335,8 @@ module MDPP # @return [String] def render text = _render(@doc.children, @doc.properties) - text += _render_bibliography unless @bibliography.empty? + text += _render_bibliography unless @bibliography.empty? or + @options['nb'] text end @@ -342,7 +344,7 @@ module MDPP def _render_bibliography size = IO.console.winsize[1] - text = "\n#{('─' * size)}\n" + text = "\n#{'─' * size}\n" text += @bibliography.map.with_index do |element, index| "- [#{index + 1}] #{wordwrap(element.join(': '), size - 15)}" end.join("\n") @@ -426,8 +428,52 @@ module MDPP end end -if __FILE__ == $0 - text = $stdin.read - renderer = MDPP::Renderer.new(text, {}) - puts renderer.render +options = {} +OptionParser.new do |opts| + opts.banner = <<~TEXT + MDPP - Markdown PrettyPrint based on RBMark parser + Usage: mdpp [options] + TEXT + + opts.on("-h", "--help", "Prints this help message") do + puts opts + exit 0 + end + + opts.on("-e", "--extension EXTENSION", + "require EXTENSION before parsing") do |libname| + require libname + end + + opts.on( + "-c", + "--config CONFIG", + "try to load CONFIG (~/.config/mdpp.rb is loaded by default)" + ) do |config| + # rubocop:disable Security/Eval + options.merge!(eval(File.read(config))) if File.exist?(config) + # rubocop:enable Security/Eval + end + + opts.on( + "-b", + "--no-bibliography", + "Do not print bibliography (links, references, etc.) at the bottom" + ) do + options["nb"] = true + end +end.parse! + +# rubocop:disable Security/Eval +if File.exist?("#{ENV['HOME']}/.config/mdpp.rb") + options.merge!(eval(File.read("#{ENV['HOME']}/.config/mdpp.rb"))) end +# rubocop:enable Security/Eval + +text = if ARGV[0].nil? or ARGV[0] == "-" + $stdin.read + else + File.read(ARGV[0]) + end +renderer = MDPP::Renderer.new(text, options) +puts renderer.render diff --git a/classes b/classes deleted file mode 100644 index 9153047..0000000 --- a/classes +++ /dev/null @@ -1,13 +0,0 @@ -Bold [x} -Italics [x] -Underline [x] -Strikethrough [x] -CodeInline [x] -Link [x] -Image [x] -Headings [x] -CodeBlock [x] -QuoteBlock [x] -ULBlock [x] -OLBLock [x] -TableBlock [] diff --git a/document.rb b/lib/rbmark.rb similarity index 100% rename from document.rb rename to lib/rbmark.rb diff --git a/rbmark.gemspec b/rbmark.gemspec new file mode 100644 index 0000000..11f5f3e --- /dev/null +++ b/rbmark.gemspec @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +Gem::Specification.new do |s| + s.name = 'rbmark' + s.version = '0.5' + s.summary = <<~SUMMARY + Modular, extensible, HTML-agnostic Markdown parser + SUMMARY + s.description = <<~TEXT + RBMark is a Markdown parser that represents Markdown in a DOM-like + object structure, allowing for other interfaces to produce more + complex translators from Markdown to any given format. + TEXT + s.authors = ['yessiest'] + s.email = 'yessiest@text.512mb.org' + s.license = 'Apache-2.0' + s.homepage = 'https://adastra7.net/git/Yessiest/rubymark' + s.files = Dir['lib/**/*.rb'] + Dir['bin/*'] + s.required_ruby_version = '>= 3.0.0' + s.executables = ['mdpp'] +end diff --git a/test.md b/test.md deleted file mode 100644 index 5c49d3b..0000000 --- a/test.md +++ /dev/null @@ -1,149 +0,0 @@ -# Header level sadga kjshdkj hasdkjs hakjdhakjshd kashd kjashd kjashdk asjhdkj ashdkj ahskj hdaskd haskj hdkjash dkjashd ksajdh askjd hak askjhdkasjhdaksjhd sakjd 1 - -> Block quote text -> -> Second block quote paragraph -> Block quote **bold** and *italics* test -> Block quote **bold *italics* mix** test - -> Nested block quote test -> > with a very stupid secondary quote syntax -this should end the block quote btw - -## Header level 2 - -also header level 2 ---- - -also header level 1 -=== - -not a header level2 ----asdlkashdlkasjd - -not a header level1 -===asdajlsdkjlsd - - not a header level2 - --- - - not a header level1 - === -[link](http://example.com) -![image alt text](http://example.com) - -``` plaintext -code *block* -eat my shit -``` - -> It is also of importance to note that this code is a hello world program, -> written entirely in C++. -> -> ```cpp -> int main() { -> cout << "Hello **world!**"; -> } -> ``` - -> theoretical failure test case -> **bold** **with space** -paragraph with ``inline code block`` - -- Unordered list element 1 -- Unordered list element 2 - -1. Ordered list element 1 -2. Ordered list element 2 - -1. [Link](https://ass.com) - 2. [Lv2](https://ass.com) - 3. [Lv2](https://anus.com) -4. shit - -This is not a list -- because it continues the paragraph -- this is how it should be, like it or not - -- This is also not a list -because there is text on the next line - -- But this here is a list - because the spacing is made correctly - - more so than that, there are multiple paragraphs here! - - - AND even more lists in a list! - - how extra -- And this is just the next element in the list - -1. same thing but with ordered lists - ordered lists have a little extra special property to them - - the indentations are always symmetrical to the last space of the bullet's number -10. i.e., if you look at this here example - this will work - - obviously - - -1. But this -10. Won't - because the indentation doesn't match the start of the line. - -generally speaking this kind of insane syntax trickery won't be necessary, -but it's just better to have standards than to have none of them. - -an unfortunate side effect of this flexibility should also be noted, and -it's that markdown linters don't like this sort of stuff. -Yet another reason not to use a markdown linter. - -- And this is just the lame stupid old way to do this, as described by mardkownguide - - > just indent your stuff and it works - > really it's as simple as that. - > bruh - - there can be as many as infinite number of elements appended to the list that way. - - you can even start a sublist here if you want to - - - here's a new nested list - - could you imagine the potential - - and here's an image of nothing - - ![image](https://example.com/nothing.png) - -- I may also need to merge lists for this to work properly - -### Third level header - -text - -#### 4th level text - -- list -- > list with blockquote - > quote - > more of the same quote - > - > - inner list - > - list2 - > - > ```plaintext - > code block inside a quote - > could you imagine that shit - > eh - > ``` - ---- - -gnomo - - indented preformatted text - indented preformatted text - function() peepeeopoopoo { - puts ass - } - indented preformatted text