diff --git a/README.md b/README.md index 6afbc16..940bddd 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,33 @@ pico Be careful not to build without any themes at all, nothing will work. +## Templates for standalone HTML output + +If you are using the HTML formatter, you can pass a template to use for the output. The template is a string where the following placeholders will be replaced: + +* `{{style_defs}}` will be replaced by the CSS styles needed for the theme +* `{{code}}` will be replaced by the highlighted code + +This is an example template that changes the padding around the code: + +```jinja2 + + + + + + + {{body}} + + +``` + + ## Contributing 1. Fork it () diff --git a/src/formatters/html.cr b/src/formatters/html.cr index 457ff36..cfefd07 100644 --- a/src/formatters/html.cr +++ b/src/formatters/html.cr @@ -28,6 +28,13 @@ module Tartrazine property? surrounding_pre : Bool = true property? wrap_long_lines : Bool = false property weight_of_bold : Int32 = 600 + property template : String = <<-TEMPLATE + +{{body}} + +TEMPLATE property theme : Theme @@ -42,7 +49,8 @@ module Tartrazine @standalone : Bool = false, @surrounding_pre : Bool = true, @wrap_long_lines : Bool = false, - @weight_of_bold : Int32 = 600) + @weight_of_bold : Int32 = 600, + @template : String = @template) end def format(text : String, lexer : Lexer) : String @@ -61,11 +69,15 @@ module Tartrazine # Wrap text into a full HTML document, including the CSS for the theme def wrap_standalone output = String.build do |outp| - outp << "" + if @template.includes? "{{style_defs}}" + outp << @template.split("{{style_defs}}")[0] + outp << style_defs + outp << @template.split("{{style_defs}}")[1].split("{{body}}")[0] + else + outp << @template.split("{{body}}")[0] + end end - {output.to_s, ""} + {output.to_s, @template.split("{{body}}")[1]} end private def line_label(i : Int32) : String diff --git a/src/main.cr b/src/main.cr index 11b375f..8cec62c 100644 --- a/src/main.cr +++ b/src/main.cr @@ -10,8 +10,8 @@ Keep in mind that not all formatters support all features. Usage: tartrazine (-h, --help) - tartrazine FILE -f html [-t theme][--standalone][--line-numbers] - [-l lexer][-o output] + tartrazine FILE -f html [-t theme][--standalone [--template file]] + [--line-numbers][-l lexer][-o output] tartrazine -f html -t theme --css tartrazine FILE -f terminal [-t theme][-l lexer][--line-numbers] [-o output] @@ -35,6 +35,7 @@ Options: all style information. If not given, it will generate just a HTML fragment ready to include in your own page. --css Generate a CSS file for the theme called .css + --template Use a custom template for the HTML output [default: none] --line-numbers Include line numbers in the output -h, --help Show this screen -v, --version Show version number @@ -64,6 +65,12 @@ if options["--list-formatters"] end theme = Tartrazine.theme(options["-t"].as(String)) +template = options["--template"].as(String) +if template != "none" # Otherwise we will use the default template + template = File.open(template).gets_to_end +else + template = nil +end if options["-f"] formatter = options["-f"].as(String) @@ -73,6 +80,7 @@ if options["-f"] formatter.standalone = options["--standalone"] != nil formatter.line_numbers = options["--line-numbers"] != nil formatter.theme = theme + formatter.template = template if template when "terminal" formatter = Tartrazine::Ansi.new formatter.line_numbers = options["--line-numbers"] != nil