Skip to content

Commit

Permalink
support for github markdown extension "alerts" (#9)
Browse files Browse the repository at this point in the history
* support for github markdown extension "alerts"
  • Loading branch information
as-op authored Jun 24, 2024
1 parent 8772c79 commit 0b12221
Show file tree
Hide file tree
Showing 20 changed files with 459 additions and 17 deletions.
29 changes: 29 additions & 0 deletions demo/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,35 @@ This is _italic_ text with `_`
> Blockquotes can be f**or**m*att*ed ~~strikethrough~~ <u>underline</u>

# Alert Blockquotes

Markdown extension by [Github](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts)

> [!NOTE]
> Useful information that users should know, even when skimming content.

> [!TIP]
> Helpful advice for doing things better or more easily.

> [!IMPORTANT]
> Key information users need to know to achieve their goal.

> [!WARNING]
> Urgent info that needs immediate user attention to avoid problems.

> [!CAUTION]
> Advises about risks or negative outcomes of certain actions.

> [!NOTE: With an own title]
> Useful information that users should know, even when skimming content.

<br-page/>

# Links
Expand Down
58 changes: 58 additions & 0 deletions demo/demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ task_list:
task_list_point:
checked: ''
unchecked: ''
font: 'NotoSansSymbols2'
spacing: 2mm
size: 9

Expand Down Expand Up @@ -251,6 +252,63 @@ blockquote:
margin_top: 2mm
margin_bottom: 2mm

alerts:
NOTE:
border_color: '0969da'
alert_color: '0969da'
padding: '4mm'
size: 10
styles: []
border_width: 2
no_border_right: true
no_border_left: false
no_border_bottom: true
no_border_top: true
TIP:
border_color: '1a7f37'
alert_color: '1a7f37'
padding: '4mm'
size: 10
styles: [ ]
border_width: 2
no_border_right: true
no_border_left: false
no_border_bottom: true
no_border_top: true
IMPORTANT:
border_color: '8250df'
alert_color: '8250df'
padding: '4mm'
size: 10
styles: [ ]
border_width: 2
no_border_right: true
no_border_left: false
no_border_bottom: true
no_border_top: true
WARNING:
border_color: 'bf8700'
alert_color: 'bf8700'
padding: '4mm'
size: 10
styles: [ ]
border_width: 2
no_border_right: true
no_border_left: false
no_border_bottom: true
no_border_top: true
CAUTION:
border_color: 'd1242f'
alert_color: 'd1242f'
size: 10
styles: [ ]
padding: '4mm'
border_width: 2
no_border_right: true
no_border_left: false
no_border_bottom: true
no_border_top: true

code:
font: 'Consolas'
# size: 9
Expand Down
41 changes: 41 additions & 0 deletions docs/STYLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,40 @@
| `task_list` | **Task list**<br/>See [Task list](#task-list) | object |
| `task_list_point` | **Task list point**<br/>See [Task list point](#task-list-point) | object |
| `fields_default` | **md-to-pdf default fields settings**<br/>See [md-to-pdf default fields settings](#md-to-pdf-default-fields-settings) | object |
| `alerts` | **alert boxes (styled blockquotes)**<br/>See [alert boxes (styled blockquotes)](#alert-boxes-styled-blockquotes) | object |
| `header_1`<br/>`header_2`<br/>`header_x` | Header level<br/>See [Header](#header) | object |
| `ordered_list_1`<br/>`ordered_list_2`<br/>`ordered_list_x` | See [Ordered list level](#ordered-list-level) | object |
| `ordered_list_point_1`<br/>`ordered_list_point_2`<br/>`ordered_list_point_x` | See [Ordered list point level](#ordered-list-point-level) | object |
| `unordered_list_1`<br/>`unordered_list_2`<br/>`unordered_list_x` | Unordered list level<br/>See [Unordered list](#unordered-list) | object |
| `unordered_list_point_1`<br/>`unordered_list_point_2`<br/>`unordered_list_point_x` | Unordered list point level<br/>See [Unordered list point](#unordered-list-point) | object |

## Alert

Styling to denote a quote as alert box

Key: `alert`

Example:
```yml
ALERT:
alert_color: f4f9ff
border_color: f4f9ff
border_width: 2
no_border_right: true
no_border_left: false
no_border_bottom: true
no_border_top: true
```
| Key | Description | Data type |
| - | - | - |
| `background_color` | **Color**<br/>A color in RRGGBB format<br/>Example: `F0F0F0` | string |
| `alert_color` | **Color**<br/>A color in RRGGBB format<br/>Example: `F0F0F0` | string |
| … | See [Font properties](#font-properties) | |
| … | See [Border properties](#border-properties) | |
| … | See [Padding properties](#padding-properties) | |
| … | See [Margin properties](#margin-properties) | |

## Blockquote

Styling to denote a paragraph as quote
Expand Down Expand Up @@ -876,6 +904,18 @@ Key: `unordered_list_point_x`
| `spacing` | **Spacing**<br/>Space between point and list item content<br/>Examples: `10mm`, `10` | number or string<br/>See [Units](#units) |
| … | See [Font properties](#font-properties) | |

## alert boxes (styled blockquotes)

Key: `alerts`

| Key | Description | Data type |
| - | - | - |
| `NOTE` | **Alert**<br/>Styling to denote a quote as alert box<br/>See [Alert](#alert) | object |
| `TIP` | **Alert**<br/>Styling to denote a quote as alert box<br/>See [Alert](#alert) | object |
| `WARNING` | **Alert**<br/>Styling to denote a quote as alert box<br/>See [Alert](#alert) | object |
| `IMPORTANT` | **Alert**<br/>Styling to denote a quote as alert box<br/>See [Alert](#alert) | object |
| `CAUTION` | **Alert**<br/>Styling to denote a quote as alert box<br/>See [Alert](#alert) | object |

## md-to-pdf default custom fields values

Key: `pdf_fields`
Expand All @@ -900,6 +940,7 @@ Key: `fields_default`
| `pdf_header_logo` | | string |
| `pdf_hyphenation` | | boolean |
| `pdf_fields` | **md-to-pdf default custom fields values**<br/>See [md-to-pdf default custom fields values](#md-to-pdf-default-custom-fields-values) | object |

## Units

available units are
Expand Down
44 changes: 39 additions & 5 deletions lib/md_to_pdf/elements/blockquote.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require 'prawn/table'

module MarkdownToPDF
module Blockquote
def data_blockquote(node, opts)
result = []
case node.type
when :paragraph
result.push(data_blockquote_paragraph(node, opts))
result.concat(data_blockquote_paragraph(node, opts, result.empty?))
when :blockquote
result.push(data_blockquote_quote(node, opts))
when :list
Expand All @@ -23,15 +25,47 @@ def draw_blockquote(node, opts)
return if data.empty?

with_block_margin_all(margin_opts) do
draw_blockquote_table(data, cell_style_opts)
draw_blockquote_table(data, cell_style_opts[:alert_style_opts] || cell_style_opts)
end
end

private

def data_blockquote_paragraph(node, opts)
cell_data = data_node_children(node, opts)
[make_table_cell_or_subtable(cell_data, opts, :left, 1)]
def data_blockquote_paragraph(node, opts, is_first)
node.to_a.each do |child|
if child.type == :softbreak
new_node = Markly::Node.new(:linebreak)
child.insert_before(new_node)
child.delete
end
end
current_opts = opts
alert_type = nil
alert_title = nil
if is_first && node.first_child && node.first_child.type == :text
line = node.first_child.string_content
line.match(/^\[!([A-Z]*)(?::([^\]]*))?\]/) do |m|
if MarkdownToPDF::Fonts::ALERT_OCTICONS.key?(m[1].to_sym)
alert_type = m[1].to_sym
alert_title = m[2] || alert_type.to_s.capitalize
opts[:alert_style_opts] = opts_alert_table_cell(@styles.alert_styles(alert_type), opts)
current_opts = opts[:alert_style_opts]
end
end
end

cell_data = data_node_children(node, current_opts)
if alert_type
cell_data[0][:text] = "#{alert_title.strip}\n"
icon = MarkdownToPDF::Fonts::ALERT_OCTICONS[alert_type]
if current_opts[:alert_color]
icon = "<color rgb=\"#{current_opts[:alert_color]}\">#{icon}</color>"
cell_data[0][:color] = current_opts[:alert_color]
end
text = "<font name=\"#{MarkdownToPDF::Fonts::ALERT_OCTICONS_FONTNAME}\">#{icon}</font> "
cell_data.insert(0, { text: text, raw: true })
end
[[make_table_cell_or_subtable(cell_data, current_opts, :left, 1)]]
end

def data_blockquote_list(node, opts)
Expand Down
4 changes: 3 additions & 1 deletion lib/md_to_pdf/elements/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ def find_img_caption(tag)
end

def cell_inline_formatting(cell_data_part)
# https://prawnpdf.org/docs/0.11.1/Prawn/Text.html internal format
return cell_data_part[:text] if cell_data_part[:raw]

# https://prawnpdf.org/docs/prawn/2.5.0/Prawn/Text.html inline format
value = cell_inline_formatting_data(cell_data_part)
cell_inline_formatting_styles(cell_data_part, value)
end
Expand Down
5 changes: 3 additions & 2 deletions lib/md_to_pdf/elements/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ def make_subtable_cell(cell_data, opts)
end

def make_table_cell(cell_data, opts)
additional_cell_settings = cell_data.empty? ? {} : cell_data[0]
Prawn::Table::Cell::Text.new(
@pdf, [0, 0],
content: merge_cell_data(cell_data),
font: opts[:font],
size: opts[:size],
padding: opts[:cell_padding],
background_color: cell_data.empty? ? nil : cell_data[0][:cell_background_color],
padding: opts[:cell_padding] || additional_cell_settings[:cell_padding],
background_color: additional_cell_settings[:cell_background_color],
inline_format: true
)
end
Expand Down
1 change: 1 addition & 0 deletions lib/md_to_pdf/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def pdf_setup_document
style = @styles.page
@pdf = Prawn::Document.new(pdf_document_options(style))
pdf_init_fonts(@pdf, @styles.fonts, @styles.fallback_font, @fonts_path)
pdf_init_md2pdf_fonts(@pdf)
end
end
end
7 changes: 7 additions & 0 deletions lib/md_to_pdf/style/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ def opts_table_cell(style, opts = {})
)
end

def opts_alert_table_cell(style, opts = {})
merge_opts(
{ alert_color: style[:alert_color] },
opts_table_cell(style, opts)
)
end

def opts_image(style)
align = (style[:align] || 'center').to_sym
{ position: align }
Expand Down
2 changes: 1 addition & 1 deletion lib/md_to_pdf/style/schema_doc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def link_to_title(title)

def measurement_section
result = []
result << "## Units\n"
result << "\n## Units\n"
result << "available units are\n"
result << "`mm` - Millimeter, `cm` - Centimeter, `dm` - Decimeter, `m` - Meter\n"
result << "`in` - Inch, `ft` - Feet, `yr` - Yard\n"
Expand Down
62 changes: 62 additions & 0 deletions lib/md_to_pdf/style/schema_styles.json
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@
},
"fields_default": {
"$ref": "#/$defs/fields_default"
},
"alerts": {
"$ref": "#/$defs/alerts"
}
},
"patternProperties": {
Expand Down Expand Up @@ -1406,6 +1409,65 @@
}
}
}
},
"alert": {
"type": "object",
"title": "Alert",
"description": "Styling to denote a quote as alert box",
"x-example": {
"ALERT": {
"alert_color": "f4f9ff",
"border_color": "f4f9ff",
"border_width": 2,
"no_border_right": true,
"no_border_left": false,
"no_border_bottom": true,
"no_border_top": true
}
},
"properties": {
"background_color": {
"$ref": "#/$defs/color"
},
"alert_color": {
"$ref": "#/$defs/color"
}
},
"allOf": [
{
"$ref": "#/$defs/font"
},
{
"$ref": "#/$defs/border"
},
{
"$ref": "#/$defs/padding"
},
{
"$ref": "#/$defs/margin"
}
]
},
"alerts": {
"type": "object",
"title": "alert boxes (styled blockquotes)",
"properties": {
"NOTE": {
"$ref": "#/$defs/alert"
},
"TIP": {
"$ref": "#/$defs/alert"
},
"WARNING": {
"$ref": "#/$defs/alert"
},
"IMPORTANT": {
"$ref": "#/$defs/alert"
},
"CAUTION": {
"$ref": "#/$defs/alert"
}
}
}
}
}
5 changes: 5 additions & 0 deletions lib/md_to_pdf/style/styles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,10 @@ def blockquote
def get_style(section)
@styling[section] || {}
end

def alert_styles(alert_type)
alert_styles = get_style(:alerts)
alert_styles[alert_type] || {}
end
end
end
Loading

0 comments on commit 0b12221

Please sign in to comment.