mirror of
https://github.com/tommilligan/mdbook-admonish.git
synced 2025-12-27 10:01:43 -05:00
initial commit working
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
2105
Cargo.lock
generated
Normal file
2105
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
18
Cargo.toml
Normal file
18
Cargo.toml
Normal file
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "mdbook-admonish"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "3.0.14", features = ["cargo"] }
|
||||
env_logger = "0.9.0"
|
||||
log = "0.4.14"
|
||||
mdbook = "0.4.15"
|
||||
pulldown-cmark = "0.9.1"
|
||||
serde_json = "1.0.79"
|
||||
toml_edit = "0.13.4"
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.1.0"
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 Tom Milligan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
53
README.md
Normal file
53
README.md
Normal file
@@ -0,0 +1,53 @@
|
||||
# mdbook-admonish
|
||||
|
||||
A preprocessor for [mdbook][] to add visual support for admonishments.
|
||||
|
||||
[mdbook]: https://github.com/rust-lang-nursery/mdBook
|
||||
|
||||
It turns this:
|
||||
|
||||
````
|
||||
```admonish
|
||||
A simple message.
|
||||
```
|
||||
````
|
||||
|
||||
into this:
|
||||
|
||||

|
||||
|
||||
in your book.
|
||||
|
||||
(Styling based on [mkdocs-material](https://github.com/squidfunk/mkdocs-material))
|
||||
|
||||
## Installation
|
||||
|
||||
If you want to use only this preprocessor, install the tool:
|
||||
|
||||
```
|
||||
cargo install mdbook-admonish
|
||||
```
|
||||
|
||||
Then let `mdbook-admonish` add the required files and configuration:
|
||||
|
||||
```
|
||||
mdbook-admonish install path/to/your/book
|
||||
```
|
||||
|
||||
This will add the following configuration to your `book.toml`:
|
||||
|
||||
```toml
|
||||
[preprocessor.admonish]
|
||||
command = "mdbook-admonish"
|
||||
|
||||
[output.html]
|
||||
additional-js = ["mdbook-admonish.css"]
|
||||
```
|
||||
|
||||
Additionally it copies the files `mdbook-admonish.css` into your book's directory.
|
||||
|
||||
Finally, build your book:
|
||||
|
||||
```
|
||||
mdbook path/to/book
|
||||
```
|
||||
373
licenses/LICENSE-mdbook-mermaid
Normal file
373
licenses/LICENSE-mdbook-mermaid
Normal file
@@ -0,0 +1,373 @@
|
||||
Mozilla Public License Version 2.0
|
||||
==================================
|
||||
|
||||
1. Definitions
|
||||
--------------
|
||||
|
||||
1.1. "Contributor"
|
||||
means each individual or legal entity that creates, contributes to
|
||||
the creation of, or owns Covered Software.
|
||||
|
||||
1.2. "Contributor Version"
|
||||
means the combination of the Contributions of others (if any) used
|
||||
by a Contributor and that particular Contributor's Contribution.
|
||||
|
||||
1.3. "Contribution"
|
||||
means Covered Software of a particular Contributor.
|
||||
|
||||
1.4. "Covered Software"
|
||||
means Source Code Form to which the initial Contributor has attached
|
||||
the notice in Exhibit A, the Executable Form of such Source Code
|
||||
Form, and Modifications of such Source Code Form, in each case
|
||||
including portions thereof.
|
||||
|
||||
1.5. "Incompatible With Secondary Licenses"
|
||||
means
|
||||
|
||||
(a) that the initial Contributor has attached the notice described
|
||||
in Exhibit B to the Covered Software; or
|
||||
|
||||
(b) that the Covered Software was made available under the terms of
|
||||
version 1.1 or earlier of the License, but not also under the
|
||||
terms of a Secondary License.
|
||||
|
||||
1.6. "Executable Form"
|
||||
means any form of the work other than Source Code Form.
|
||||
|
||||
1.7. "Larger Work"
|
||||
means a work that combines Covered Software with other material, in
|
||||
a separate file or files, that is not Covered Software.
|
||||
|
||||
1.8. "License"
|
||||
means this document.
|
||||
|
||||
1.9. "Licensable"
|
||||
means having the right to grant, to the maximum extent possible,
|
||||
whether at the time of the initial grant or subsequently, any and
|
||||
all of the rights conveyed by this License.
|
||||
|
||||
1.10. "Modifications"
|
||||
means any of the following:
|
||||
|
||||
(a) any file in Source Code Form that results from an addition to,
|
||||
deletion from, or modification of the contents of Covered
|
||||
Software; or
|
||||
|
||||
(b) any new file in Source Code Form that contains any Covered
|
||||
Software.
|
||||
|
||||
1.11. "Patent Claims" of a Contributor
|
||||
means any patent claim(s), including without limitation, method,
|
||||
process, and apparatus claims, in any patent Licensable by such
|
||||
Contributor that would be infringed, but for the grant of the
|
||||
License, by the making, using, selling, offering for sale, having
|
||||
made, import, or transfer of either its Contributions or its
|
||||
Contributor Version.
|
||||
|
||||
1.12. "Secondary License"
|
||||
means either the GNU General Public License, Version 2.0, the GNU
|
||||
Lesser General Public License, Version 2.1, the GNU Affero General
|
||||
Public License, Version 3.0, or any later versions of those
|
||||
licenses.
|
||||
|
||||
1.13. "Source Code Form"
|
||||
means the form of the work preferred for making modifications.
|
||||
|
||||
1.14. "You" (or "Your")
|
||||
means an individual or a legal entity exercising rights under this
|
||||
License. For legal entities, "You" includes any entity that
|
||||
controls, is controlled by, or is under common control with You. For
|
||||
purposes of this definition, "control" means (a) the power, direct
|
||||
or indirect, to cause the direction or management of such entity,
|
||||
whether by contract or otherwise, or (b) ownership of more than
|
||||
fifty percent (50%) of the outstanding shares or beneficial
|
||||
ownership of such entity.
|
||||
|
||||
2. License Grants and Conditions
|
||||
--------------------------------
|
||||
|
||||
2.1. Grants
|
||||
|
||||
Each Contributor hereby grants You a world-wide, royalty-free,
|
||||
non-exclusive license:
|
||||
|
||||
(a) under intellectual property rights (other than patent or trademark)
|
||||
Licensable by such Contributor to use, reproduce, make available,
|
||||
modify, display, perform, distribute, and otherwise exploit its
|
||||
Contributions, either on an unmodified basis, with Modifications, or
|
||||
as part of a Larger Work; and
|
||||
|
||||
(b) under Patent Claims of such Contributor to make, use, sell, offer
|
||||
for sale, have made, import, and otherwise transfer either its
|
||||
Contributions or its Contributor Version.
|
||||
|
||||
2.2. Effective Date
|
||||
|
||||
The licenses granted in Section 2.1 with respect to any Contribution
|
||||
become effective for each Contribution on the date the Contributor first
|
||||
distributes such Contribution.
|
||||
|
||||
2.3. Limitations on Grant Scope
|
||||
|
||||
The licenses granted in this Section 2 are the only rights granted under
|
||||
this License. No additional rights or licenses will be implied from the
|
||||
distribution or licensing of Covered Software under this License.
|
||||
Notwithstanding Section 2.1(b) above, no patent license is granted by a
|
||||
Contributor:
|
||||
|
||||
(a) for any code that a Contributor has removed from Covered Software;
|
||||
or
|
||||
|
||||
(b) for infringements caused by: (i) Your and any other third party's
|
||||
modifications of Covered Software, or (ii) the combination of its
|
||||
Contributions with other software (except as part of its Contributor
|
||||
Version); or
|
||||
|
||||
(c) under Patent Claims infringed by Covered Software in the absence of
|
||||
its Contributions.
|
||||
|
||||
This License does not grant any rights in the trademarks, service marks,
|
||||
or logos of any Contributor (except as may be necessary to comply with
|
||||
the notice requirements in Section 3.4).
|
||||
|
||||
2.4. Subsequent Licenses
|
||||
|
||||
No Contributor makes additional grants as a result of Your choice to
|
||||
distribute the Covered Software under a subsequent version of this
|
||||
License (see Section 10.2) or under the terms of a Secondary License (if
|
||||
permitted under the terms of Section 3.3).
|
||||
|
||||
2.5. Representation
|
||||
|
||||
Each Contributor represents that the Contributor believes its
|
||||
Contributions are its original creation(s) or it has sufficient rights
|
||||
to grant the rights to its Contributions conveyed by this License.
|
||||
|
||||
2.6. Fair Use
|
||||
|
||||
This License is not intended to limit any rights You have under
|
||||
applicable copyright doctrines of fair use, fair dealing, or other
|
||||
equivalents.
|
||||
|
||||
2.7. Conditions
|
||||
|
||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
|
||||
in Section 2.1.
|
||||
|
||||
3. Responsibilities
|
||||
-------------------
|
||||
|
||||
3.1. Distribution of Source Form
|
||||
|
||||
All distribution of Covered Software in Source Code Form, including any
|
||||
Modifications that You create or to which You contribute, must be under
|
||||
the terms of this License. You must inform recipients that the Source
|
||||
Code Form of the Covered Software is governed by the terms of this
|
||||
License, and how they can obtain a copy of this License. You may not
|
||||
attempt to alter or restrict the recipients' rights in the Source Code
|
||||
Form.
|
||||
|
||||
3.2. Distribution of Executable Form
|
||||
|
||||
If You distribute Covered Software in Executable Form then:
|
||||
|
||||
(a) such Covered Software must also be made available in Source Code
|
||||
Form, as described in Section 3.1, and You must inform recipients of
|
||||
the Executable Form how they can obtain a copy of such Source Code
|
||||
Form by reasonable means in a timely manner, at a charge no more
|
||||
than the cost of distribution to the recipient; and
|
||||
|
||||
(b) You may distribute such Executable Form under the terms of this
|
||||
License, or sublicense it under different terms, provided that the
|
||||
license for the Executable Form does not attempt to limit or alter
|
||||
the recipients' rights in the Source Code Form under this License.
|
||||
|
||||
3.3. Distribution of a Larger Work
|
||||
|
||||
You may create and distribute a Larger Work under terms of Your choice,
|
||||
provided that You also comply with the requirements of this License for
|
||||
the Covered Software. If the Larger Work is a combination of Covered
|
||||
Software with a work governed by one or more Secondary Licenses, and the
|
||||
Covered Software is not Incompatible With Secondary Licenses, this
|
||||
License permits You to additionally distribute such Covered Software
|
||||
under the terms of such Secondary License(s), so that the recipient of
|
||||
the Larger Work may, at their option, further distribute the Covered
|
||||
Software under the terms of either this License or such Secondary
|
||||
License(s).
|
||||
|
||||
3.4. Notices
|
||||
|
||||
You may not remove or alter the substance of any license notices
|
||||
(including copyright notices, patent notices, disclaimers of warranty,
|
||||
or limitations of liability) contained within the Source Code Form of
|
||||
the Covered Software, except that You may alter any license notices to
|
||||
the extent required to remedy known factual inaccuracies.
|
||||
|
||||
3.5. Application of Additional Terms
|
||||
|
||||
You may choose to offer, and to charge a fee for, warranty, support,
|
||||
indemnity or liability obligations to one or more recipients of Covered
|
||||
Software. However, You may do so only on Your own behalf, and not on
|
||||
behalf of any Contributor. You must make it absolutely clear that any
|
||||
such warranty, support, indemnity, or liability obligation is offered by
|
||||
You alone, and You hereby agree to indemnify every Contributor for any
|
||||
liability incurred by such Contributor as a result of warranty, support,
|
||||
indemnity or liability terms You offer. You may include additional
|
||||
disclaimers of warranty and limitations of liability specific to any
|
||||
jurisdiction.
|
||||
|
||||
4. Inability to Comply Due to Statute or Regulation
|
||||
---------------------------------------------------
|
||||
|
||||
If it is impossible for You to comply with any of the terms of this
|
||||
License with respect to some or all of the Covered Software due to
|
||||
statute, judicial order, or regulation then You must: (a) comply with
|
||||
the terms of this License to the maximum extent possible; and (b)
|
||||
describe the limitations and the code they affect. Such description must
|
||||
be placed in a text file included with all distributions of the Covered
|
||||
Software under this License. Except to the extent prohibited by statute
|
||||
or regulation, such description must be sufficiently detailed for a
|
||||
recipient of ordinary skill to be able to understand it.
|
||||
|
||||
5. Termination
|
||||
--------------
|
||||
|
||||
5.1. The rights granted under this License will terminate automatically
|
||||
if You fail to comply with any of its terms. However, if You become
|
||||
compliant, then the rights granted under this License from a particular
|
||||
Contributor are reinstated (a) provisionally, unless and until such
|
||||
Contributor explicitly and finally terminates Your grants, and (b) on an
|
||||
ongoing basis, if such Contributor fails to notify You of the
|
||||
non-compliance by some reasonable means prior to 60 days after You have
|
||||
come back into compliance. Moreover, Your grants from a particular
|
||||
Contributor are reinstated on an ongoing basis if such Contributor
|
||||
notifies You of the non-compliance by some reasonable means, this is the
|
||||
first time You have received notice of non-compliance with this License
|
||||
from such Contributor, and You become compliant prior to 30 days after
|
||||
Your receipt of the notice.
|
||||
|
||||
5.2. If You initiate litigation against any entity by asserting a patent
|
||||
infringement claim (excluding declaratory judgment actions,
|
||||
counter-claims, and cross-claims) alleging that a Contributor Version
|
||||
directly or indirectly infringes any patent, then the rights granted to
|
||||
You by any and all Contributors for the Covered Software under Section
|
||||
2.1 of this License shall terminate.
|
||||
|
||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
|
||||
end user license agreements (excluding distributors and resellers) which
|
||||
have been validly granted by You or Your distributors under this License
|
||||
prior to termination shall survive termination.
|
||||
|
||||
************************************************************************
|
||||
* *
|
||||
* 6. Disclaimer of Warranty *
|
||||
* ------------------------- *
|
||||
* *
|
||||
* Covered Software is provided under this License on an "as is" *
|
||||
* basis, without warranty of any kind, either expressed, implied, or *
|
||||
* statutory, including, without limitation, warranties that the *
|
||||
* Covered Software is free of defects, merchantable, fit for a *
|
||||
* particular purpose or non-infringing. The entire risk as to the *
|
||||
* quality and performance of the Covered Software is with You. *
|
||||
* Should any Covered Software prove defective in any respect, You *
|
||||
* (not any Contributor) assume the cost of any necessary servicing, *
|
||||
* repair, or correction. This disclaimer of warranty constitutes an *
|
||||
* essential part of this License. No use of any Covered Software is *
|
||||
* authorized under this License except under this disclaimer. *
|
||||
* *
|
||||
************************************************************************
|
||||
|
||||
************************************************************************
|
||||
* *
|
||||
* 7. Limitation of Liability *
|
||||
* -------------------------- *
|
||||
* *
|
||||
* Under no circumstances and under no legal theory, whether tort *
|
||||
* (including negligence), contract, or otherwise, shall any *
|
||||
* Contributor, or anyone who distributes Covered Software as *
|
||||
* permitted above, be liable to You for any direct, indirect, *
|
||||
* special, incidental, or consequential damages of any character *
|
||||
* including, without limitation, damages for lost profits, loss of *
|
||||
* goodwill, work stoppage, computer failure or malfunction, or any *
|
||||
* and all other commercial damages or losses, even if such party *
|
||||
* shall have been informed of the possibility of such damages. This *
|
||||
* limitation of liability shall not apply to liability for death or *
|
||||
* personal injury resulting from such party's negligence to the *
|
||||
* extent applicable law prohibits such limitation. Some *
|
||||
* jurisdictions do not allow the exclusion or limitation of *
|
||||
* incidental or consequential damages, so this exclusion and *
|
||||
* limitation may not apply to You. *
|
||||
* *
|
||||
************************************************************************
|
||||
|
||||
8. Litigation
|
||||
-------------
|
||||
|
||||
Any litigation relating to this License may be brought only in the
|
||||
courts of a jurisdiction where the defendant maintains its principal
|
||||
place of business and such litigation shall be governed by laws of that
|
||||
jurisdiction, without reference to its conflict-of-law provisions.
|
||||
Nothing in this Section shall prevent a party's ability to bring
|
||||
cross-claims or counter-claims.
|
||||
|
||||
9. Miscellaneous
|
||||
----------------
|
||||
|
||||
This License represents the complete agreement concerning the subject
|
||||
matter hereof. If any provision of this License is held to be
|
||||
unenforceable, such provision shall be reformed only to the extent
|
||||
necessary to make it enforceable. Any law or regulation which provides
|
||||
that the language of a contract shall be construed against the drafter
|
||||
shall not be used to construe this License against a Contributor.
|
||||
|
||||
10. Versions of the License
|
||||
---------------------------
|
||||
|
||||
10.1. New Versions
|
||||
|
||||
Mozilla Foundation is the license steward. Except as provided in Section
|
||||
10.3, no one other than the license steward has the right to modify or
|
||||
publish new versions of this License. Each version will be given a
|
||||
distinguishing version number.
|
||||
|
||||
10.2. Effect of New Versions
|
||||
|
||||
You may distribute the Covered Software under the terms of the version
|
||||
of the License under which You originally received the Covered Software,
|
||||
or under the terms of any subsequent version published by the license
|
||||
steward.
|
||||
|
||||
10.3. Modified Versions
|
||||
|
||||
If you create software not governed by this License, and you want to
|
||||
create a new license for such software, you may create and use a
|
||||
modified version of this License if you rename the license and remove
|
||||
any references to the name of the license steward (except to note that
|
||||
such modified license differs from this License).
|
||||
|
||||
10.4. Distributing Source Code Form that is Incompatible With Secondary
|
||||
Licenses
|
||||
|
||||
If You choose to distribute Source Code Form that is Incompatible With
|
||||
Secondary Licenses under the terms of this version of the License, the
|
||||
notice described in Exhibit B of this License must be attached.
|
||||
|
||||
Exhibit A - Source Code Form License Notice
|
||||
-------------------------------------------
|
||||
|
||||
This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
If it is not possible or desirable to put the notice in a particular
|
||||
file, then You may include the notice in a location (such as a LICENSE
|
||||
file in a relevant directory) where a recipient would be likely to look
|
||||
for such a notice.
|
||||
|
||||
You may add additional accurate notices of copyright ownership.
|
||||
|
||||
Exhibit B - "Incompatible With Secondary Licenses" Notice
|
||||
---------------------------------------------------------
|
||||
|
||||
This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
defined by the Mozilla Public License, v. 2.0.
|
||||
19
licenses/LICENSE-mkdocs-material
Normal file
19
licenses/LICENSE-mkdocs-material
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2016-2022 Martin Donath <martin.donath@squidfunk.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to
|
||||
deal in the Software without restriction, including without limitation the
|
||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
sell copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
8
licenses/README.md
Normal file
8
licenses/README.md
Normal file
@@ -0,0 +1,8 @@
|
||||
# Licenses
|
||||
|
||||
This utility is heavily drawn from and inspired by other projects, namely:
|
||||
|
||||
- [mdbook-mermaid](https://github.com/badboy/mdbook-mermaid)
|
||||
- [mkdocs-material](https://github.com/squidfunk/mkdocs-material)
|
||||
|
||||
The licences for these projects are included here.
|
||||
108
src/bin/assets/mdbook-admonish.css
Normal file
108
src/bin/assets/mdbook-admonish.css
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Based on Admonitions for MkDocs, which is based on MUI design.
|
||||
*
|
||||
* https://squidfunk.github.io/mkdocs-material/reference/admonitions/#supported-types
|
||||
*/
|
||||
|
||||
/*
|
||||
* Base classes for admonitions.
|
||||
*/
|
||||
.admonition {
|
||||
background-color: var(--bg);
|
||||
border: 0 solid var(--fg);
|
||||
border-left-width: 0.4rem;
|
||||
border-radius: 0.2rem;
|
||||
box-shadow: 0 0.4rem 1.0rem #0000000d,0 0 0.1rem rgba(0,0,0,.1);
|
||||
color: var(--fg);
|
||||
display: flow-root;
|
||||
margin: 1.5625em 0;
|
||||
padding: 0 1.2rem;
|
||||
page-break-inside: avoid;
|
||||
}
|
||||
|
||||
|
||||
.admonition-title {
|
||||
border-top-left-radius: 0.2rem;
|
||||
border-left-width: 0.4rem;
|
||||
padding-left: 4rem;
|
||||
padding-right: 1.2rem;
|
||||
margin-left: -1.6rem;
|
||||
margin-right: -1.2rem;
|
||||
font-weight: bold;
|
||||
margin-bottom: 0;
|
||||
margin-top: 0;
|
||||
padding-bottom: 0.8rem;
|
||||
padding-top: 0.8rem;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.admonition-title:before {
|
||||
content: "";
|
||||
display: block;
|
||||
background-color: var(--fg);
|
||||
mask-image: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"></svg>');
|
||||
-webkit-mask-image: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"></svg>');
|
||||
mask-repeat: no-repeat;
|
||||
-webkit-mask-repeat: no-repeat;
|
||||
mask-size: contain;
|
||||
-webkit-mask-size: contain;
|
||||
content: "";
|
||||
height: 2rem;
|
||||
position: absolute;
|
||||
top: 0.625em;
|
||||
width: 2rem;
|
||||
left: 1.2rem;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Overrides for the different types of admonitions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* note
|
||||
*/
|
||||
:root {
|
||||
--md-admonition-icon--note: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20.71 7.04c.39-.39.39-1.04 0-1.41l-2.34-2.34c-.37-.39-1.02-.39-1.41 0l-1.84 1.83 3.75 3.75M3 17.25V21h3.75L17.81 9.93l-3.75-3.75L3 17.25z"/></svg>');
|
||||
--md-admonition-color--note: #448aff;
|
||||
--md-admonition-color-rgb--note: 68, 138, 255;
|
||||
}
|
||||
|
||||
.admonition.note {
|
||||
border-color: var(--md-admonition-color--note);
|
||||
}
|
||||
|
||||
:is(.note)>:is(.admonition-title) {
|
||||
background-color: rgba(var(--md-admonition-color-rgb--note), .1);
|
||||
}
|
||||
|
||||
:is(.note)>:is(.admonition-title):before {
|
||||
background-color: var(--md-admonition-color--note);
|
||||
mask-image: var(--md-admonition-icon--note);
|
||||
-webkit-mask-image: var(--md-admonition-icon--note);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* warning
|
||||
*/
|
||||
:root {
|
||||
--md-admonition-icon--warning: url('data:image/svg+xml;charset=utf-8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M13 14h-2V9h2m0 9h-2v-2h2M1 21h22L12 2 1 21z"/></svg>');
|
||||
--md-admonition-color--warning: #ff9100;
|
||||
--md-admonition-color-rgb--warning: 255, 145, 0;
|
||||
}
|
||||
|
||||
.admonition.warning {
|
||||
border-color: var(--md-admonition-color--warning);
|
||||
}
|
||||
|
||||
:is(.warning)>:is(.admonition-title) {
|
||||
background-color: rgba(var(--md-admonition-color-rgb--warning), .1);
|
||||
}
|
||||
|
||||
:is(.warning)>:is(.admonition-title):before {
|
||||
background-color: var(--md-admonition-color--warning);
|
||||
mask-image: var(--md-admonition-icon--warning);
|
||||
-webkit-mask-image: var(--md-admonition-icon--warning);
|
||||
}
|
||||
|
||||
228
src/bin/mdbook-admonish.rs
Normal file
228
src/bin/mdbook-admonish.rs
Normal file
@@ -0,0 +1,228 @@
|
||||
use clap::{crate_version, Arg, ArgMatches, Command};
|
||||
use mdbook::errors::Error;
|
||||
use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
|
||||
use mdbook_admonish::Admonish;
|
||||
use toml_edit::{value, Array, Document, Item, Table, Value};
|
||||
|
||||
use std::{
|
||||
fs::{self, File},
|
||||
io::{self, Write},
|
||||
path::PathBuf,
|
||||
process,
|
||||
};
|
||||
|
||||
const ADMONISH_JS: &[u8] = include_bytes!("assets/mdbook-admonish.css");
|
||||
const ADMONISH_FILES: &[(&str, &[u8])] = &[("mdbook-admonish.css", ADMONISH_JS)];
|
||||
|
||||
pub fn make_app() -> Command<'static> {
|
||||
Command::new("mdbook-admonish")
|
||||
.version(crate_version!())
|
||||
.about("mdbook preprocessor to add admonish support")
|
||||
.subcommand(
|
||||
Command::new("supports")
|
||||
.arg(Arg::new("renderer").required(true))
|
||||
.about("Check whether a renderer is supported by this preprocessor"),
|
||||
)
|
||||
.subcommand(
|
||||
Command::new("install")
|
||||
.arg(
|
||||
Arg::new("dir")
|
||||
.default_value(".")
|
||||
.help("Root directory for the book,\nshould contain the configuration file (`book.toml`)")
|
||||
)
|
||||
.about("Install the required assset files and include it in the config"),
|
||||
)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
env_logger::init_from_env(env_logger::Env::default().default_filter_or("info"));
|
||||
|
||||
let matches = make_app().get_matches();
|
||||
|
||||
if let Some(sub_args) = matches.subcommand_matches("supports") {
|
||||
handle_supports(sub_args);
|
||||
} else if let Some(sub_args) = matches.subcommand_matches("install") {
|
||||
handle_install(sub_args);
|
||||
} else if let Err(e) = handle_preprocessing() {
|
||||
eprintln!("{}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_preprocessing() -> Result<(), Error> {
|
||||
let (ctx, book) = CmdPreprocessor::parse_input(io::stdin())?;
|
||||
|
||||
if ctx.mdbook_version != mdbook::MDBOOK_VERSION {
|
||||
eprintln!(
|
||||
"Warning: The mdbook-admonish preprocessor was built against version \
|
||||
{} of mdbook, but we're being called from version {}",
|
||||
mdbook::MDBOOK_VERSION,
|
||||
ctx.mdbook_version
|
||||
);
|
||||
}
|
||||
|
||||
let processed_book = Admonish.run(&ctx, book)?;
|
||||
serde_json::to_writer(io::stdout(), &processed_book)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_supports(sub_args: &ArgMatches) -> ! {
|
||||
let renderer = sub_args.value_of("renderer").expect("Required argument");
|
||||
let supported = Admonish.supports_renderer(renderer);
|
||||
|
||||
// Signal whether the renderer is supported by exiting with 1 or 0.
|
||||
if supported {
|
||||
process::exit(0);
|
||||
} else {
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_install(sub_args: &ArgMatches) -> ! {
|
||||
let dir = sub_args.value_of("dir").expect("Required argument");
|
||||
let proj_dir = PathBuf::from(dir);
|
||||
let config = proj_dir.join("book.toml");
|
||||
|
||||
if !config.exists() {
|
||||
log::error!("Configuration file '{}' missing", config.display());
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
log::info!("Reading configuration file {}", config.display());
|
||||
let toml = fs::read_to_string(&config).expect("can't read configuration file");
|
||||
let mut doc = toml
|
||||
.parse::<Document>()
|
||||
.expect("configuration is not valid TOML");
|
||||
|
||||
let has_pre = has_preprocessor(&mut doc);
|
||||
if !has_pre {
|
||||
log::info!("Adding preprocessor configuration");
|
||||
add_preprocessor(&mut doc);
|
||||
}
|
||||
|
||||
let added_files = add_additional_files(&mut doc);
|
||||
|
||||
if !has_pre || added_files {
|
||||
log::info!("Saving changed configuration to {}", config.display());
|
||||
let toml = doc.to_string();
|
||||
let mut file = File::create(config).expect("can't open configuration file for writing.");
|
||||
file.write_all(toml.as_bytes())
|
||||
.expect("can't write configuration");
|
||||
}
|
||||
|
||||
let mut printed = false;
|
||||
for (name, content) in ADMONISH_FILES {
|
||||
let filepath = proj_dir.join(name);
|
||||
if filepath.exists() {
|
||||
log::debug!(
|
||||
"'{}' already exists (Path: {}). Skipping.",
|
||||
name,
|
||||
filepath.display()
|
||||
);
|
||||
} else {
|
||||
if !printed {
|
||||
printed = true;
|
||||
log::info!(
|
||||
"Writing additional files to project directory at {}",
|
||||
proj_dir.display()
|
||||
);
|
||||
}
|
||||
log::debug!("Writing content for '{}' into {}", name, filepath.display());
|
||||
let mut file = File::create(filepath).expect("can't open file for writing");
|
||||
file.write_all(content)
|
||||
.expect("can't write content to file");
|
||||
}
|
||||
}
|
||||
|
||||
log::info!("Files & configuration for mdbook-admonish are installed. You can start using it in your book.");
|
||||
let codeblock = r#"```admonish-warning
|
||||
A beautifully styled message.
|
||||
```"#;
|
||||
log::info!("Add a code block like:\n{}", codeblock);
|
||||
|
||||
process::exit(0);
|
||||
}
|
||||
|
||||
fn add_additional_files(doc: &mut Document) -> bool {
|
||||
let mut changed = false;
|
||||
|
||||
let file = "mdbook-admonish.css";
|
||||
let additional_js = additional(doc, "js");
|
||||
if has_file(&additional_js, file) {
|
||||
log::debug!("'{}' already in 'additional-js'. Skipping", file)
|
||||
} else {
|
||||
log::info!("Adding additional files to configuration");
|
||||
log::debug!("Adding '{}' to 'additional-js'", file);
|
||||
insert_additional(doc, "js", file);
|
||||
changed = true;
|
||||
}
|
||||
|
||||
changed
|
||||
}
|
||||
|
||||
fn additional<'a>(doc: &'a mut Document, additional_type: &str) -> Option<&'a mut Array> {
|
||||
let doc = doc.as_table_mut();
|
||||
|
||||
let item = doc.get_mut("output")?;
|
||||
let item = item.as_table_mut()?.get_mut("html")?;
|
||||
let item = item
|
||||
.as_table_mut()?
|
||||
.get_mut(&format!("additional-{}", additional_type))?;
|
||||
item.as_array_mut()
|
||||
}
|
||||
|
||||
fn has_preprocessor(doc: &mut Document) -> bool {
|
||||
doc.get("preprocessor")
|
||||
.and_then(|p| p.get("admonish"))
|
||||
.map(|m| matches!(m, Item::Table(_)))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
fn add_preprocessor(doc: &mut Document) {
|
||||
let doc = doc.as_table_mut();
|
||||
|
||||
let empty_table = Item::Table(Table::default());
|
||||
|
||||
let item = doc.entry("preprocessor").or_insert(empty_table.clone());
|
||||
let item = item
|
||||
.as_table_mut()
|
||||
.unwrap()
|
||||
.entry("admonish")
|
||||
.or_insert(empty_table);
|
||||
item["command"] = value("mdbook-admonish");
|
||||
}
|
||||
|
||||
fn has_file(elem: &Option<&mut Array>, file: &str) -> bool {
|
||||
match elem {
|
||||
Some(elem) => elem.iter().any(|elem| match elem.as_str() {
|
||||
None => true,
|
||||
Some(s) => s.ends_with(file),
|
||||
}),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn insert_additional(doc: &mut Document, additional_type: &str, file: &str) {
|
||||
let doc = doc.as_table_mut();
|
||||
|
||||
let empty_table = Item::Table(Table::default());
|
||||
let empty_array = Item::Value(Value::Array(Array::default()));
|
||||
let item = doc.entry("output").or_insert(empty_table.clone());
|
||||
let item = item
|
||||
.as_table_mut()
|
||||
.unwrap()
|
||||
.entry("html")
|
||||
.or_insert(empty_table);
|
||||
let array = item
|
||||
.as_table_mut()
|
||||
.unwrap()
|
||||
.entry(&format!("additional-{}", additional_type))
|
||||
.or_insert(empty_array);
|
||||
let _ = array
|
||||
.as_value_mut()
|
||||
.unwrap()
|
||||
.as_array_mut()
|
||||
.unwrap()
|
||||
.push(file);
|
||||
}
|
||||
237
src/lib.rs
Normal file
237
src/lib.rs
Normal file
@@ -0,0 +1,237 @@
|
||||
use mdbook::book::{Book, BookItem, Chapter};
|
||||
use mdbook::errors::Result;
|
||||
use mdbook::preprocess::{Preprocessor, PreprocessorContext};
|
||||
use pulldown_cmark::{CodeBlockKind::*, Event, Options, Parser, Tag};
|
||||
|
||||
pub struct Admonish;
|
||||
|
||||
impl Preprocessor for Admonish {
|
||||
fn name(&self) -> &str {
|
||||
"admonish"
|
||||
}
|
||||
|
||||
fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result<Book> {
|
||||
let mut res = None;
|
||||
book.for_each_mut(|item: &mut BookItem| {
|
||||
if let Some(Err(_)) = res {
|
||||
return;
|
||||
}
|
||||
|
||||
if let BookItem::Chapter(ref mut chapter) = *item {
|
||||
res = Some(Admonish::add_admonish(chapter).map(|md| {
|
||||
chapter.content = md;
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
res.unwrap_or(Ok(())).map(|_| book)
|
||||
}
|
||||
|
||||
fn supports_renderer(&self, renderer: &str) -> bool {
|
||||
renderer == "html"
|
||||
}
|
||||
}
|
||||
|
||||
fn escape_html(s: &str) -> String {
|
||||
let mut output = String::new();
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'<' => output.push_str("<"),
|
||||
'>' => output.push_str(">"),
|
||||
'"' => output.push_str("""),
|
||||
'&' => output.push_str("&"),
|
||||
_ => output.push(c),
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
fn add_admonish(content: &str) -> Result<String> {
|
||||
let mut admonish_content = String::new();
|
||||
let mut in_admonish_block = false;
|
||||
|
||||
let mut opts = Options::empty();
|
||||
opts.insert(Options::ENABLE_TABLES);
|
||||
opts.insert(Options::ENABLE_FOOTNOTES);
|
||||
opts.insert(Options::ENABLE_STRIKETHROUGH);
|
||||
opts.insert(Options::ENABLE_TASKLISTS);
|
||||
|
||||
let mut admonish_start = 0..0;
|
||||
|
||||
let mut admonish_blocks = vec![];
|
||||
|
||||
let events = Parser::new_ext(content, opts);
|
||||
for (e, span) in events.into_offset_iter() {
|
||||
if let Event::Start(Tag::CodeBlock(Fenced(code))) = e.clone() {
|
||||
log::debug!("e={:?}, span={:?}", e, span);
|
||||
if &*code == "admonish" {
|
||||
admonish_start = span;
|
||||
in_admonish_block = true;
|
||||
admonish_content.clear();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if !in_admonish_block {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Event::End(Tag::CodeBlock(Fenced(code))) = e {
|
||||
assert_eq!(
|
||||
"admonish", &*code,
|
||||
"After an opening admonish code block we expect it to close again"
|
||||
);
|
||||
in_admonish_block = false;
|
||||
let pre = "```admonish\n";
|
||||
let post = "```";
|
||||
|
||||
let admonish_content =
|
||||
&content[admonish_start.start + pre.len()..span.end - post.len()];
|
||||
let admonish_content = escape_html(admonish_content);
|
||||
let admonish_content = admonish_content.trim();
|
||||
let admonish_code = format!(
|
||||
r#"<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<p>{admonish_content}</p>
|
||||
</div>"#
|
||||
);
|
||||
admonish_blocks.push((admonish_start.start..span.end, admonish_code.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
let mut content = content.to_string();
|
||||
for (span, block) in admonish_blocks.iter().rev() {
|
||||
let pre_content = &content[0..span.start];
|
||||
let post_content = &content[span.end..];
|
||||
content = format!("{}\n{}{}", pre_content, block, post_content);
|
||||
}
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
impl Admonish {
|
||||
fn add_admonish(chapter: &mut Chapter) -> Result<String> {
|
||||
add_admonish(&chapter.content)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::add_admonish;
|
||||
|
||||
#[test]
|
||||
fn adds_admonish() {
|
||||
let content = r#"# Chapter
|
||||
```admonish
|
||||
A simple admonition.
|
||||
```
|
||||
Text
|
||||
"#;
|
||||
|
||||
let expected = r#"# Chapter
|
||||
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<p>A simple admonition.</p>
|
||||
</div>
|
||||
Text
|
||||
"#;
|
||||
|
||||
assert_eq!(expected, add_admonish(content).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaves_tables_untouched() {
|
||||
// Regression test.
|
||||
// Previously we forgot to enable the same markdwon extensions as mdbook itself.
|
||||
|
||||
let content = r#"# Heading
|
||||
| Head 1 | Head 2 |
|
||||
|--------|--------|
|
||||
| Row 1 | Row 2 |
|
||||
"#;
|
||||
|
||||
let expected = r#"# Heading
|
||||
| Head 1 | Head 2 |
|
||||
|--------|--------|
|
||||
| Row 1 | Row 2 |
|
||||
"#;
|
||||
|
||||
assert_eq!(expected, add_admonish(content).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaves_html_untouched() {
|
||||
// Regression test.
|
||||
// Don't remove important newlines for syntax nested inside HTML
|
||||
|
||||
let content = r#"# Heading
|
||||
<del>
|
||||
*foo*
|
||||
</del>
|
||||
"#;
|
||||
|
||||
let expected = r#"# Heading
|
||||
<del>
|
||||
*foo*
|
||||
</del>
|
||||
"#;
|
||||
|
||||
assert_eq!(expected, add_admonish(content).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn html_in_list() {
|
||||
// Regression test.
|
||||
// Don't remove important newlines for syntax nested inside HTML
|
||||
|
||||
let content = r#"# Heading
|
||||
1. paragraph 1
|
||||
```
|
||||
code 1
|
||||
```
|
||||
2. paragraph 2
|
||||
"#;
|
||||
|
||||
let expected = r#"# Heading
|
||||
1. paragraph 1
|
||||
```
|
||||
code 1
|
||||
```
|
||||
2. paragraph 2
|
||||
"#;
|
||||
|
||||
assert_eq!(expected, add_admonish(content).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_in_admonish_block() {
|
||||
env_logger::init();
|
||||
let content = r#"
|
||||
```admonish
|
||||
classDiagram
|
||||
class PingUploader {
|
||||
<<interface>>
|
||||
+Upload() UploadResult
|
||||
}
|
||||
```
|
||||
hello
|
||||
"#;
|
||||
|
||||
let expected = r#"
|
||||
|
||||
<div class="admonition note">
|
||||
<p class="admonition-title">Note</p>
|
||||
<p>classDiagram
|
||||
class PingUploader {
|
||||
<<interface>>
|
||||
+Upload() UploadResult
|
||||
}</p>
|
||||
</div>
|
||||
hello
|
||||
"#;
|
||||
|
||||
assert_eq!(expected, add_admonish(content).unwrap());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user