From 7331d4addff6ca00910202444610f0d2f16ccb2e Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Mon, 26 Aug 2019 09:04:32 -0300 Subject: [PATCH] Revert "Move Markdown to inside the compiler. (#8115)" This reverts commit 491f26b10cf67c1131aa035c4d720b53f30eab72. --- .../doc => std/markdown}/markdown_spec.cr | 6 ++-- src/compiler/crystal/tools/doc/generator.cr | 2 +- .../crystal/tools/doc/markdown/markdown.cr | 36 ------------------- ...c_renderer.cr => markdown_doc_renderer.cr} | 8 ++--- .../crystal/tools/playground/server.cr | 4 +-- src/docs_main.cr | 1 + src/markdown.cr | 29 +++++++++++++++ .../tools/doc => }/markdown/html_renderer.cr | 2 +- .../crystal/tools/doc => }/markdown/parser.cr | 2 +- .../tools/doc => }/markdown/renderer.cr | 2 +- 10 files changed, 42 insertions(+), 50 deletions(-) rename spec/{compiler/crystal/tools/doc => std/markdown}/markdown_spec.cr (97%) delete mode 100644 src/compiler/crystal/tools/doc/markdown/markdown.cr rename src/compiler/crystal/tools/doc/{markdown/doc_renderer.cr => markdown_doc_renderer.cr} (96%) create mode 100644 src/markdown.cr rename src/{compiler/crystal/tools/doc => }/markdown/html_renderer.cr (97%) rename src/{compiler/crystal/tools/doc => }/markdown/parser.cr (99%) rename src/{compiler/crystal/tools/doc => }/markdown/renderer.cr (94%) diff --git a/spec/compiler/crystal/tools/doc/markdown_spec.cr b/spec/std/markdown/markdown_spec.cr similarity index 97% rename from spec/compiler/crystal/tools/doc/markdown_spec.cr rename to spec/std/markdown/markdown_spec.cr index 1868cb814468..27009ebe427c 100644 --- a/spec/compiler/crystal/tools/doc/markdown_spec.cr +++ b/spec/std/markdown/markdown_spec.cr @@ -1,13 +1,13 @@ require "spec" -require "../../../../../src/compiler/crystal/tools/doc/markdown" +require "markdown" private def assert_render(input, output, file = __FILE__, line = __LINE__) it "renders #{input.inspect}", file, line do - Crystal::Doc::Markdown.to_html(input).should eq(output), file, line + Markdown.to_html(input).should eq(output), file, line end end -describe Crystal::Doc::Markdown do +describe Markdown do assert_render "", "" assert_render "Hello", "

Hello

" assert_render "Hello\nWorld", "

Hello\nWorld

" diff --git a/src/compiler/crystal/tools/doc/generator.cr b/src/compiler/crystal/tools/doc/generator.cr index ccad5e3bb7ac..578d6723cb8d 100644 --- a/src/compiler/crystal/tools/doc/generator.cr +++ b/src/compiler/crystal/tools/doc/generator.cr @@ -303,7 +303,7 @@ class Crystal::Doc::Generator string = isolate_flag_lines string string += build_flag_lines_from_annotations context markdown = String.build do |io| - Markdown.parse string, Markdown::DocRenderer.new(context, io) + Markdown.parse string, MarkdownDocRenderer.new(context, io) end generate_flags markdown end diff --git a/src/compiler/crystal/tools/doc/markdown/markdown.cr b/src/compiler/crystal/tools/doc/markdown/markdown.cr deleted file mode 100644 index ebfaf869a75d..000000000000 --- a/src/compiler/crystal/tools/doc/markdown/markdown.cr +++ /dev/null @@ -1,36 +0,0 @@ -# Basic implementation of Markdown for the `crystal doc` tool. -# -# It lacks many features and it has some bugs too. Eventually we should replace -# it with something more feature-complete (like https://github.com/icyleaf/markd) -# but that means the compiler will start depending on external shards. Otherwise -# we should extract the doc as a separate tool/binary. -# We don't expose this library in the standard library because it's probable -# that we will never make it feature complete. -# -# Usage: -# -# ``` -# require "compiler/crystal/tools/doc/markdown" -# -# text = "## This is title \n This is a [link](http://crystal-lang.org)" -# -# Crystal::Doc::Markdown.to_html(text) -# # =>

This is title

-# # =>

This is a link

-# ``` -module Crystal::Doc::Markdown - def self.parse(text, renderer) - parser = Parser.new(text, renderer) - parser.parse - end - - def self.to_html(text) : String - String.build do |io| - parse text, Markdown::HTMLRenderer.new(io) - end - end -end - -require "./parser" -require "./renderer" -require "./html_renderer" diff --git a/src/compiler/crystal/tools/doc/markdown/doc_renderer.cr b/src/compiler/crystal/tools/doc/markdown_doc_renderer.cr similarity index 96% rename from src/compiler/crystal/tools/doc/markdown/doc_renderer.cr rename to src/compiler/crystal/tools/doc/markdown_doc_renderer.cr index de5bed317c14..3c46be773491 100644 --- a/src/compiler/crystal/tools/doc/markdown/doc_renderer.cr +++ b/src/compiler/crystal/tools/doc/markdown_doc_renderer.cr @@ -1,13 +1,11 @@ -require "./*" +require "markdown" -class Crystal::Doc::Markdown::DocRenderer < Crystal::Doc::Markdown::HTMLRenderer +class Crystal::Doc::MarkdownDocRenderer < Markdown::HTMLRenderer def self.new(obj : Constant | Macro | Method, io) new obj.type, io end - @type : Crystal::Doc::Type - - def initialize(@type : Crystal::Doc::Type, io) + def initialize(@type : Type, io) super(io) @inside_inline_code = false diff --git a/src/compiler/crystal/tools/playground/server.cr b/src/compiler/crystal/tools/playground/server.cr index 7cc303a1819e..9b50f1d6884c 100644 --- a/src/compiler/crystal/tools/playground/server.cr +++ b/src/compiler/crystal/tools/playground/server.cr @@ -1,8 +1,8 @@ require "http/server" require "logger" require "ecr/macros" +require "markdown" require "compiler/crystal/tools/formatter" -require "compiler/crystal/tools/doc/markdown" module Crystal::Playground class Session @@ -247,7 +247,7 @@ module Crystal::Playground end if extname == ".md" || extname == ".cr" - content = Crystal::Doc::Markdown.to_html(content) + content = Markdown.to_html(content) end content rescue e diff --git a/src/docs_main.cr b/src/docs_main.cr index c84fb0ecd71e..6fec82fcfc7b 100644 --- a/src/docs_main.cr +++ b/src/docs_main.cr @@ -19,6 +19,7 @@ require "./llvm" require "./logger" require "./macros" require "./math/**" +require "./markdown" require "./oauth" require "./oauth2" require "./openssl" diff --git a/src/markdown.cr b/src/markdown.cr new file mode 100644 index 000000000000..79d63d171a2f --- /dev/null +++ b/src/markdown.cr @@ -0,0 +1,29 @@ +# Markdown library parses Markdown text. It supports rendering to HTML text. +# +# Usage: +# +# ``` +# require "markdown" +# +# text = "## This is title \n This is a [link](http://crystal-lang.org)" +# +# Markdown.to_html(text) +# # =>

This is title

+# # =>

This is a link

+# ``` +# +# NOTE: This library is in its early stage. Many features are still in development. +class Markdown + def self.parse(text, renderer) + parser = Parser.new(text, renderer) + parser.parse + end + + def self.to_html(text) : String + String.build do |io| + parse text, Markdown::HTMLRenderer.new(io) + end + end +end + +require "./markdown/*" diff --git a/src/compiler/crystal/tools/doc/markdown/html_renderer.cr b/src/markdown/html_renderer.cr similarity index 97% rename from src/compiler/crystal/tools/doc/markdown/html_renderer.cr rename to src/markdown/html_renderer.cr index e2f17ff1abb2..093a1a3c186f 100644 --- a/src/compiler/crystal/tools/doc/markdown/html_renderer.cr +++ b/src/markdown/html_renderer.cr @@ -1,6 +1,6 @@ require "./renderer" -class Crystal::Doc::Markdown::HTMLRenderer +class Markdown::HTMLRenderer include Renderer def initialize(@io : IO) diff --git a/src/compiler/crystal/tools/doc/markdown/parser.cr b/src/markdown/parser.cr similarity index 99% rename from src/compiler/crystal/tools/doc/markdown/parser.cr rename to src/markdown/parser.cr index 9ce80dc60058..fb3355d95d60 100644 --- a/src/compiler/crystal/tools/doc/markdown/parser.cr +++ b/src/markdown/parser.cr @@ -1,4 +1,4 @@ -class Crystal::Doc::Markdown::Parser +class Markdown::Parser record PrefixHeader, count : Int32 record UnorderedList, char : Char record CodeFence, language : String diff --git a/src/compiler/crystal/tools/doc/markdown/renderer.cr b/src/markdown/renderer.cr similarity index 94% rename from src/compiler/crystal/tools/doc/markdown/renderer.cr rename to src/markdown/renderer.cr index fbb439c19fd5..daeaa07053bd 100644 --- a/src/compiler/crystal/tools/doc/markdown/renderer.cr +++ b/src/markdown/renderer.cr @@ -1,4 +1,4 @@ -module Crystal::Doc::Markdown::Renderer +module Markdown::Renderer abstract def begin_paragraph abstract def end_paragraph abstract def begin_italic