|
1 | | -# Microdown APIThere is a lot of functionality in Microdown, and this document gives an overview of the Microdown architecture.- Architecture - key objects in Microdown- Microdown facade methods- Resource references and resolvers. Dealing with links, images, and included documentsSeveral parts of Microdown allows for extending the Microdown tool suite. [Extension has is own documentation page](extension.md).The full suite of microdown tools are not loaded by default. In particular there is also support for - Translation of Microdown to Latex and Beamer- Translation of Microdown to HTML- Translation from Pillar to Microdown- Misc other tools## ArchitectureThe overall idea in Microdown is like this:$$%commentYou can edit the figure using this source on planttext.com @startumlskinparam rectangle { roundCorner 20}rectangle "Microdown" { rectangle Source <<String>> rectangle Document <<Tree>> rectangle Text <<Output>> rectangle Latex <<Output>> rectangle HTML <<Output>> Source --> Document : Parser Document --> Text : Visitor Document --> Latex : Visitor Document --> HTML : Visitor }@enduml $$The format of the source code is explained in the [section on syntax](syntax.md).The parser translates the source code into an abstract syntax tree which we refer to as a "document". Translation into various output formats are done using visitors. By default only the visitor for translation into pharo `Text` is in the image. Sometimes `Text` is refered to as `RichText` to emphasize its difference from plain character `String`.## Microdown facade### Parse and output generationThe class `Microdown` has facade methods for the main actions in the architecture.- `Microdown class >> #parse:` translates a microdown source string into a document.- `Microdown class >> #asRichText:` translates a document into rich text. `asRichText:` can also accept a source string directly, cirumventing the `parse:` method. There are two facade methods for _resolution_. - `Microdown class >> #resolveDocument:withBase:` and - `Microdown class >> #resolveString:withBase:`The will be explained in the next section ## Resolution and referencesReferences to other documents or images can be either _absolute_ or _relative_. A typical absolute reference is `[Pharo](https:pharo.org)` or ``. Absolute references cause no problems for the visitors to find the image or follow the link.However, when writing larger documents it is customary to keep images for the document in a seperate image folder, and refer to the images as ``. Such a reference is _relative_. It is relative to to location of the document containing the reference.**Resolution** is the process of replacing all relative references with absolute references. This process takes two inputs:- A document D containing relative references- The absolute location of document D (the base of the document).The two methods - `Microdown class >> #resolveDocument:withBase:` and - `Microdown class >> #resolveString:withBase:`are the prefered way to resolve documents.### Resource referencesIf we go one level below this, references are first order objects, all subclasses of `MicResourceReference`.In the standard image there are three kinds of absolute references:- http (and https) - for example: 'https://pharo.org/web/files/pharo.png'- file - for example 'file:/user/joe/docs/api.md'- the pharo image - for example 'pharo:///Object/iconNamed:/thumbsUp' ()#### Creating referencesThe prefered way is to use the extension method `asMicResourceReference`, which is defined for- String - for example `'file:/user/joe/docs/api.md' asMicResourceReference`- ZnUrl - for example `(ZnUri fromString: 'https://pharo.org/web/files/pharo.png') asMicResourceReference`- FileReference - for example `(FileSystem memory) asMicResourceReference`#### Using referencesThere are two main key methods on references:- `loadImage` - returns a `Form` by reading the image (read using `ImageReadWriter class >> #formFromStream:`)- `loadMicrodown` - loads _and resolves_ a document#### BackgroundThe resolution is done using `ZnUrl >> #withRelativeReference:`, which implements the full resolution standard [RFC 3986 Section 5](https://datatracker.ietf.org/doc/html/rfc3986#section-5).##### Ambiguity of file uriIn pharo there is an issue that uri ' file://path/to/my/document.md' is ambiguous, because it can not be determined in which file system the path is supposed to be resolved (memory or disk). This problem is dealt with, in the following manner:- `' file://path/to/my/document.md' asMicResourceReference` will always use the disk file system- `aFileReference asMicResourceReference` will create a `MicFileResourceReference` which remembers the file system in the file reference.- `aMicFileResourceReference loadDocument` will use the right filesystem for resolution. |
| 1 | +# Microdown API |
| 2 | + |
| 3 | +There is a lot of functionality in Microdown, and this document gives an overview of the Microdown architecture. |
| 4 | + |
| 5 | +- Architecture - key objects in Microdown. |
| 6 | +- Microdown facade methods. |
| 7 | +- Resource references and resolvers. Dealing with links, images, and included documents. |
| 8 | +- Document checkers: Microdown provides checkers that validate documents. |
| 9 | + |
| 10 | +Several parts of Microdown allows for extending the Microdown tool suite. Chapter *@[Extension](extension.md)@* presents the mechanisms in place. |
| 11 | + |
| 12 | +The full suite of microdown tools are not loaded by default. In particular there is also support for: |
| 13 | +- Translation of Microdown to Latex and Beamer, |
| 14 | +- Translation of Microdown to HTML, |
| 15 | +- Translation from Pillar to Microdown, and |
| 16 | +- Document checkers. |
| 17 | + |
| 18 | +## Architecture |
| 19 | +The overall idea in Microdown is like this: |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +You can edit the figure using this source on planttext.com |
| 24 | +``` |
| 25 | +@startuml |
| 26 | +skinparam rectangle { |
| 27 | + roundCorner 20 |
| 28 | +} |
| 29 | +rectangle "Microdown" { |
| 30 | + rectangle Source <<String>> |
| 31 | + rectangle Document <<Tree>> |
| 32 | + rectangle Text <<Output>> |
| 33 | + rectangle Latex <<Output>> |
| 34 | + rectangle HTML <<Output>> |
| 35 | + Source --> Document : Parser |
| 36 | + Document --> Text : Visitor |
| 37 | + Document --> Latex : Visitor |
| 38 | + Document --> HTML : Visitor |
| 39 | +} |
| 40 | +@enduml |
| 41 | +``` |
| 42 | + |
| 43 | +The format of the source code is explained in the Chapter *@[Syntax](syntax.md)@*. |
| 44 | + |
| 45 | +The parser translates the source code into an abstract syntax tree which we refer to as a "document". Translation into various output formats are done using visitors. By default only the visitor for translation into Pharo `Text` objects is in the image. Sometimes `Text` is refered to as `RichText` to emphasize its difference from plain character `String`. |
| 46 | + |
| 47 | + |
| 48 | +## Microdown facade |
| 49 | +### Parse and output generation |
| 50 | +The class `Microdown` has facade methods for the main actions in the architecture. |
| 51 | + |
| 52 | +- `Microdown class >> #parse:` translates a microdown source string into a document. |
| 53 | +- `Microdown class >> #asRichText:` translates a document into rich text. `asRichText:` can also accept a source string directly, cirumventing the `parse:` method. |
| 54 | + |
| 55 | +There are two facade methods for _resolution_. |
| 56 | +- `Microdown class >> #resolveDocument:withBase:` and |
| 57 | +- `Microdown class >> #resolveString:withBase:` |
| 58 | + |
| 59 | +The will be explained in the next section. |
| 60 | + |
| 61 | +## Resolution and references |
| 62 | + |
| 63 | +References to other documents or images can be either _absolute_ or _relative_. |
| 64 | + |
| 65 | +#### Absolute vs. relative references. |
| 66 | + |
| 67 | +A typical absolute reference is `[Pharo](https:pharo.org)` or ``. Absolute references cause no problems for the visitors to find the image or follow the link. |
| 68 | +However, when writing larger documents it is customary to keep images for the document in a seperate image folder, and refer to the images as ``. Such a reference is _relative_. It is relative to to location of the document containing the reference. |
| 69 | + |
| 70 | +**Resolution** is the process of replacing all relative references with absolute references. This process takes two inputs: |
| 71 | +- A document D containing relative references and |
| 72 | +- The absolute location of document D (the base of the document). |
| 73 | + |
| 74 | +The two methods |
| 75 | +- `Microdown class >> #resolveDocument:withBase:` and |
| 76 | +- `Microdown class >> #resolveString:withBase:` |
| 77 | +are the prefered way to resolve documents. |
| 78 | + |
| 79 | +### Resource references |
| 80 | + |
| 81 | +If we go one level below this, references are first order objects, all subclasses of `MicResourceReference`. |
| 82 | + |
| 83 | +In the standard image there are three kinds of absolute references: |
| 84 | +- http (and https) - for example: 'https://pharo.org/web/files/pharo.png' |
| 85 | +- file - for example 'file:/user/joe/docs/api.md' |
| 86 | +- the pharo image - for example 'pharo:///Object/iconNamed:/thumbsUp' `()` |
| 87 | +- |
| 88 | +#### Creating references |
| 89 | + |
| 90 | +The prefered way is to use the extension method `asMicResourceReference`, which is defined for |
| 91 | +- String - for example `'file:/user/joe/docs/api.md' asMicResourceReference` |
| 92 | +- ZnUrl - for example `(ZnUri fromString: 'https://pharo.org/web/files/pharo.png') asMicResourceReference` |
| 93 | +- FileReference - for example `(FileSystem memory) asMicResourceReference` |
| 94 | + |
| 95 | +#### Using references |
| 96 | + |
| 97 | +There are two main key methods on references: |
| 98 | +- `loadImage` - returns a `Form` by reading the image (read using `ImageReadWriter class >> #formFromStream:`) |
| 99 | +- `loadMicrodown` - loads _and resolves_ a document |
| 100 | + |
| 101 | +#### Background |
| 102 | + |
| 103 | +The resolution is done using `ZnUrl >> #withRelativeReference:`, which implements the full resolution standard [RFC 3986 Section 5](https://datatracker.ietf.org/doc/html/rfc3986#section-5). |
| 104 | + |
| 105 | +##### Ambiguity of file uri |
| 106 | + |
| 107 | +In pharo there is an issue that uri ' file://path/to/my/document.md' is ambiguous, because it can not be determined in which file system the path is supposed to be resolved (memory or disk). This problem is dealt with, in the following manner: |
| 108 | +- `' file://path/to/my/document.md' asMicResourceReference` will always use the disk file system |
| 109 | +- `aFileReference asMicResourceReference` will create a `MicFileResourceReference` which remembers the file system in the file reference. |
| 110 | +- `aMicFileResourceReference loadDocument` will use the right filesystem for resolution. |
| 111 | + |
0 commit comments