-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
169 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
public struct HTMLRenderer { | ||
private let markdown: Markdown | ||
|
||
public init(markdown: Markdown) { | ||
self.markdown = markdown | ||
} | ||
|
||
public func render() -> String { | ||
""" | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>\(markdown.title)</title> | ||
<style> | ||
body { | ||
font-family: sans-serif; | ||
color: #333; | ||
} | ||
.bold { | ||
font-weight: bold; | ||
} | ||
.italic { | ||
font-style: italic; | ||
} | ||
.underline { | ||
text-decoration: underline; | ||
} | ||
pre { | ||
background-color: #f4f4f4; | ||
padding: 10px; | ||
border-radius: 5px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
\(markdown.blocks.map { render($0) }.joined(separator: "\n")) | ||
</body> | ||
</html> | ||
""" | ||
} | ||
|
||
private func render(_ block: Block) -> String { | ||
switch block { | ||
case let .p(text): | ||
"<p>\(text.map { render($0) }.joined())</p>" | ||
case let .text(value, style): | ||
"<span class=\"\(style)\">\(value)</span>" | ||
case let .list(blocks): | ||
"<ul>\(blocks.map { "<li>\(render($0))</li>" }.joined())</ul>" | ||
case let .code(value): | ||
"<pre><code>\(value)</code></pre>" | ||
case let .h(level, blocks): | ||
"<h\(level.rawValue)>\(blocks.map { render($0) }.joined())</h\(level.rawValue)>" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters