Compare commits

...

2 Commits

5 changed files with 279 additions and 7 deletions

View File

@ -1,3 +1,59 @@
# rubymark
MMMD (Mark My Message Down)
============
(Originally titled Rubymark)
Modular, compliant Markdown parser in Ruby
Installation
------------
This package is available as a gem over at
[rubygems.org](https://rubygems.org/gems/mmmd).
Installing it is as simple as executing `gem install mmmd`
Usage
-----
This package is generally intended as a library, but it also
includes a CLI tool which permits the usage of the library
for simple document translation tasks.
Examples:
```sh
# Render the file in a terminal-oriented format
$ mmmdpp file.md -
# Read the markdown contents directly from input, output to stdout
$ external-program | mmmdpp - -
# Render file.md to a complete webpage
$ mmmdpp -r HTML file.md file.html
# Render file.md into a complete webpage and add extra tags to head and
# wrap all images with a figure tag with figcaption containing image title
$ mmmdpp -r HTML -o '"head": ["<meta charset=\"UTF-8\">", "<style>img { max-width: 90%; }</style>"]' -o '"mapping"."PointBlank::DOM::InlineImage".figcaption: true' - -
# Render file.md into a set of HTML tags, without anything extra
$ mmmdpp -r HTML -o '"nowrap": true' file.md file.html
```
A lot more usage options are documented on the Wiki page for the project
License
-------
Copyright 2025 yessiest@text.512mb.org
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -128,11 +128,10 @@ module MMMD
def initialize(overrides)
@mapping = self.class.mapping
if overrides["mapping"]
overrides["mapping"].each do |key, value|
next unless @mapping[key]
@mapping[key] = @mapping[key].merge(value)
end
overrides["mapping"]&.each do |key, value|
next unless @mapping[key]
@mapping[key] = @mapping[key].merge(value)
end
end

View File

@ -381,7 +381,11 @@ module MMMD
def initialize(overrides)
@style = self.class.style
@effect_priority = self.class.effect_priority
@style = @style.merge(overrides["style"]) if overrides["style"]
overrides["style"]&.each do |key, value|
next unless @style[key]
@style[key] = @style[key].merge(value)
end
end
attr_reader :style, :effect_priority

82
wiki/renderers/HTML.md Normal file
View File

@ -0,0 +1,82 @@
HTML renderer
=============
HTML renderer does exactly what it says on the tin - renders markdown to HTML.
It offers the ability to modify the way output is generated, as well as tags
which are used for every block.
Global options
--------------
Global options are applied to the root of the configuration hash for the
renderer. They can be applied using the following pattern via command
line:
```
$ mmmdpp -o '"nameOfGlobalOption": <value, in JSON element form>' ...
# i.e.
$ mmmdpp -o '"linewrap": 65' ...
```
Following global options can be provided:
- `linewrap` - line wrapping, in number of text columns (80 by default)
- `init_level` - initial indent level of generated text (2 by default)
- `indent` - number of spaces per indent (2 by default)
- `nowrap` - do not output wrapping code, only direct translations of
markdown elements (false by default)
- `head` - array of head elements to add to the default template
output ([] by default)
- `preambule` - text contents to embed before the translation output
(part of template containing head element by default)
- `postambule` - text contents to embed after the translation output
(part of template by default)
Per-class overrides
-------------------
Applying a per-class override via command line options works like this:
```
# see the following paragraph for all known block classes
$ mmmdpp -o '"mapping"."Block::ClassName".override: <value, in JSON element form>' ...
# i.e.
$ mmmdpp -o '"mapping"."PointBlank::DOM::Paragraph".inline: true' ...
```
For library usage, these options roughly translate to the following hash, passed
as the second argument to object initializer:
```
{
"mapping" => {
"PointBlank::DOM::Paragraph" => {
inline: true
}
}
}
```
Following options can be applied to every class element:
- `tag` - name of the tag this class should be mapped to. (i.e.
'PointBlank::DOM::Paragraph' => 'p', 'PointBlank::DOM::InlineEmphasis' =>
'em')
- `sanitize` - sanitize entities in text. shouldn't really be used anywhere
outside of text
- `inline` - the tag should be considered self-closing
- `codeblock` - special case. disables wordwrap, makes the block uninlined
regardless of containing tags.
- `figcaption` - wrap tag into a `<figure>` tag. text contained in the tag is
copied into a caption.
- `outer` - hash of parameters for an outer wrapping tag. can be defined
recursively. all options in this list apply.
- `href` - add a href attribute. works only on links and classes containing
a `:uri` attribute
- `title` - add a title attribute. works only on classes that have a
`:title` attribute
- `style` - define an inline CSS style to embed into the tag.
- `src` - add an src attribute. works only on images and classes containing
a `:uri` attribute
- `alt` - add an alt attribute. works only on classes that have a
`:title` attribute

131
wiki/renderers/Plainterm.md Normal file
View File

@ -0,0 +1,131 @@
Plainterm renderer
==================
Plainterm renderer renders markdown documents into a prettier format for reading
in the terminal. It uses certain control codes to make it work. Styles applied
to various elements can be changed.
Applicable global options
-------------------------
Global options are applied to the root of the configuration hash for the
renderer. They can be applied using the following pattern via command
line:
```
$ mmmdpp -o '"nameOfGlobalOption": <value, in JSON element form>' ...
# i.e.
$ mmmdpp -o '"hsize": 65' ...
```
- `style` - override the style of certain elements. See "Style overrides"
- `hsize` - horizontal size to align the contents to. Automatically
set to the width of the current terminal by default, or, if unavailable,
to 80 columns.
Style overrides
---------------
Style overrides provide per-class overrides for element style. It's essentially
a stylesheet applied to the element.
Applying a style override via command line options works like this:
```
# see the following paragraph for all known block classes
$ mmmdpp -o '"style"."Block::ClassName".override: <value, in JSON element form>' ...
# i.e.
$ mmmdpp -o '"style"."PointBlank::DOM::Paragraph".indent: false' ...
```
For library usage, these options roughly translate to the following hash, passed
as the second argument to object initializer:
```
{
"style" => {
"PointBlank::DOM::Paragraph" => {
indent: false
}
}
}
```
Applicable style overrides:
- `indent` (boolean) - increase indentation
- `increase_level` (boolean) - decrease horizontal space occupied (needed with
indent)
- `center` (boolean) - center text
- `bold` (boolean) - render text in bold
- `italics` (boolean) - render text in italics
- `strikethrough` (boolean) - render text with strikethrough
- `bg` (text, #RGB) - set color background
- `fg` (text, #RGB) - set color foreground
- `box` (boolean) - render contents in an ascii box
- `rjust` (boolean) - right-justify text
- `extra_newlines` (boolean) - add extra newlines around the text block
- `underline_block` (boolean) - underline text block, from left visual boundary
of text to right visual boundary of text
- `underline_full_block` (boolean) - underline text block, from left border to
right border
- `bullet` (boolean) - add bullet to block, used for bullet lists
- `numbered` (boolean) - add numbered point to block, used for ordered lists
- `leftline` (boolean) - draw a line on the left side of the block, top to
bottom
Style defaults
--------------
These are the defaults applied to each class of text block
- `PointBlank::DOM::Paragraph`:
- `indent`
- `increase_level`
- `PointBlank::DOM::Text`:
- none applied by default
- `PointBlank::DOM::SetextHeading1` (underline style of heading in markdown,
level 1)
- `center`
- `bold`
- `extra_newlines`
- `underline_full_block`
- `PointBlank::DOM::SetextHeading2` (underline style of heading in markdown,
level 2)
- `center`
- `underline_block`
- `PointBlank::DOM::ATXHeading1` (hash-symbol prefix style of heading,
level 1)
- (same as SetextHeading1)
- `PointBlank::DOM::ATXHeading2` (hash-symbol heading, level 2)
- (same as SetextHeading2)
- `PointBlank::DOM::ATXHeading3` (hash-symbol heading, level 3)
- `underline`
- `bold`
- `PointBlank::DOM::ATXHeading4` (hash-symbol heading, level 4)
- `bold`
- `underline`
- `PointBlank::DOM::ATXHeading5` (hash-symbol heading, level 5)
- `underline`
- `PointBlank::DOM::ATXHeading6` (hash-symbol heading, level 6)
- `underline`
- `PointBlank::DOM::InlineImage` (image link)
- `underline`
- `PointBlank::DOM::InlineLink` (link)
- `underline`
- `PointBlank::DOM::InlinePre` (inline code)
- none by default
- `PointBlank::DOM::InlineEmphasis`
- `italics`
- `PointBlank::DOM::InlineStrong` (strong emphasis)
- `bold`
- `PointBlank::DOM::ULListElement` (element of an unordered list)
- `bullet`
- `increase_level`
- `PointBlank::DOM::OLListElement` (element of an ordered list)
- `numbered`
- `increase_level`
- `PointBlank::DOM::QuoteBlock`
- `leftline`
- `increase_level`
- `PointBlank::DOM::HorizontalRule`
- `hrule`