Skip to content

Commit

Permalink
Move Markdown to inside the compiler. (crystal-lang#8115)
Browse files Browse the repository at this point in the history
The Markdown module is buggy and not feature complete.
We shouldn't expose it in the standard library. For the compiler's
purposes (`crystal doc`) it might be good but users should be encouraged
to use a better library. Eventually the `crystal doc` tool should
somehow use a different, better markdown library.
  • Loading branch information
asterite authored and dnamsons committed Jan 10, 2020
1 parent 59083c5 commit 6d6a5d0
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require "spec"
require "markdown"
require "../../../../../src/compiler/crystal/tools/doc/markdown"

private def assert_render(input, output, file = __FILE__, line = __LINE__)
it "renders #{input.inspect}", file, line do
Markdown.to_html(input).should eq(output), file, line
Crystal::Doc::Markdown.to_html(input).should eq(output), file, line
end
end

describe Markdown do
describe Crystal::Doc::Markdown do
assert_render "", ""
assert_render "Hello", "<p>Hello</p>"
assert_render "Hello\nWorld", "<p>Hello\nWorld</p>"
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/doc/generator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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, MarkdownDocRenderer.new(context, io)
Markdown.parse string, Markdown::DocRenderer.new(context, io)
end
generate_flags markdown
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require "markdown"
require "./*"

class Crystal::Doc::MarkdownDocRenderer < Markdown::HTMLRenderer
class Crystal::Doc::Markdown::DocRenderer < Crystal::Doc::Markdown::HTMLRenderer
def self.new(obj : Constant | Macro | Method, io)
new obj.type, io
end

def initialize(@type : Type, io)
@type : Crystal::Doc::Type

def initialize(@type : Crystal::Doc::Type, io)
super(io)

@inside_inline_code = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./renderer"

class Markdown::HTMLRenderer
class Crystal::Doc::Markdown::HTMLRenderer
include Renderer

def initialize(@io : IO)
Expand Down
36 changes: 36 additions & 0 deletions src/compiler/crystal/tools/doc/markdown/markdown.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 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)
# # => <h2>This is title</h2>
# # => <p>This is a <a href="http://crystal-lang.org">link</a></p>
# ```
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"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Markdown::Parser
class Crystal::Doc::Markdown::Parser
record PrefixHeader, count : Int32
record UnorderedList, char : Char
record CodeFence, language : String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Markdown::Renderer
module Crystal::Doc::Markdown::Renderer
abstract def begin_paragraph
abstract def end_paragraph
abstract def begin_italic
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/tools/playground/server.cr
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -247,7 +247,7 @@ module Crystal::Playground
end

if extname == ".md" || extname == ".cr"
content = Markdown.to_html(content)
content = Crystal::Doc::Markdown.to_html(content)
end
content
rescue e
Expand Down
1 change: 0 additions & 1 deletion src/docs_main.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require "./llvm"
require "./logger"
require "./macros"
require "./math/**"
require "./markdown"
require "./oauth"
require "./oauth2"
require "./openssl"
Expand Down
29 changes: 0 additions & 29 deletions src/markdown.cr

This file was deleted.

0 comments on commit 6d6a5d0

Please sign in to comment.