aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Peter McGoron 2025-01-25 15:37:19 -0500
committerGravatar Peter McGoron 2025-01-25 15:37:19 -0500
commitb0e41b9af1f9ce89fe6ef90686abd1dac6b86c10 (patch)
treea162e0445b007d9e9fdc117a702cfd58e136cd27
block structure
-rw-r--r--COPYING202
-rw-r--r--README.md71
-rw-r--r--market/blocks.scm128
-rw-r--r--market/blocks.sld27
-rw-r--r--market/default.scm186
-rw-r--r--market/default.sld28
-rw-r--r--market/string.scm127
-rw-r--r--market/string.sld28
-rw-r--r--mcgoron/srfi/61.sld66
-rw-r--r--prelude.scm28
10 files changed, 891 insertions, 0 deletions
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ 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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..44425e8
--- /dev/null
+++ b/README.md
@@ -0,0 +1,71 @@
+# Market
+
+Market is a dialect of Markdown written in portable Scheme. It is designed
+to be easy to extend and understand.
+
+It is not a [CommonMark][1] parser. For a Scheme CommonMark parser, see
+[Guile CommonMark][2].
+
+[1]: https://spec.commonmark.org/
+[2]: https://github.com/OrangeShark/guile-commonmark/
+
+## Block Syntax
+
+Market uses [significant indentation][3] to make documents more regular and
+to make parsing easier.
+
+[3]: https://en.wikipedia.org/wiki/Off-side_rule
+
+Market parse line by line, similar to CommonMark. It also parses blocks
+before inline syntax. To parse blocks:
+
+1. The beginning of each line is checked for the correct indentation
+ recursively from the top of the document tree. (Unlike other Markdowns,
+ *lazy lines are not allowed*.) Indentation is usually space, but can
+ include `>`, `|`, etc.
+2. The rest of the line is checked for a block start.
+3. If there is no block start, then it is a paragraph line.
+
+In practice, each block has a `continues` procedure that checks if the
+current fragment of the line continues the block. If it does, then the
+prefix of the line is consumed and the process continues with the active
+child block of the block. After going as far possible, the line is then
+checked with a `starts` procedure (stored in each block) to see if any
+new block can be started.
+
+Todo: add no-paragraph blocks?
+
+## Inline Syntax
+
+(This section is not yet implemented.)
+
+CommonMark's rules for inline syntax are very complex, and the authors of
+it know:
+
+> By far the trickiest part of inline parsing is handling emphasis,
+> strong emphasis, links, and images.
+
+The inline matcher for Market has a list of inline syntax and the action
+to be taken after reading each inline syntax. Upon reading an inline
+syntax (disambiguated using greedy match), the stored procedure is then
+called with the rest of the line as an argument.
+
+The parser treats `*`, `**`, and `***` as three separate cases. Combining
+this with the greedy-match rule causes the following behavior:
+
+1. Starting with `***` and mixing `**` and `*` is not allowed. Example:
+
+ ***bold and italic** just italic*
+
+ is not allowed.
+2. Contrary to the [original markdown syntax][4], `*` and `_` can be
+ combined. This allows for something like the above pattern:
+
+ _**bold and italic** just italic_
+
+ or
+
+ *emphasized text _with emphasis in it_ and out of it*
+
+ [4]: https://daringfireball.net/projects/markdown/syntax#em
+
diff --git a/market/blocks.scm b/market/blocks.scm
new file mode 100644
index 0000000..f80dec5
--- /dev/null
+++ b/market/blocks.scm
@@ -0,0 +1,128 @@
+#| Copyright 2024 Peter McGoron
+ |
+ | 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.
+ |#
+
+(define-record-type <block-node>
+ ;; A <BLOCK-NODE> contains information on a block in a document that may
+ ;; contain other blocks.
+ ;;
+ ;; DATA is arbitrary data associated with the node.
+ ;;
+ ;; (CONTINUES LINE) is a procedure that takes a line and returns either
+ ;;
+ ;; 1) `#f`, denoting that the line does not continue the block, or
+ ;; 2) the rest of the after consuming the continuation text of the block.
+ ;;
+ ;; ALLOWED-INSIDE is a procedure that takes a line and returns
+ ;;
+ ;; 1) `#f`, meaning that the line does not start a new node that can be
+ ;; started inside of the block, or
+ ;; 2) (VALUES NEW-CHILD NEW-NODE LINE), which returns
+ ;;
+ ;; 1) The new child node,
+ ;; 2) The node from which to start processing the rest of the line,
+ ;; 3) the rest of the line.
+ ;;
+ ;; CHILDREN is an SRFI-117 queue of child blocks of the node.
+ ;;
+ ;; ACTIVE is the active child in CHILDREN, or `#f` for no active child.
+ (make-block-node data continues allowed-inside children active)
+ block-node?
+ (data node-data)
+ (continues get-continues)
+ (allowed-inside get-allowed-inside set-allowed-inside!)
+ (children node-children)
+ (active get-active-child set-active-child!))
+
+(cond-expand
+ (chicken
+ (begin
+ (set-record-printer!
+ <block-node>
+ (lambda (x port)
+ (write `(block-node (data ,(node-data x))
+ (children ,@(list-queue-list (node-children x))))
+ port)
+ (newline port)))))
+ (else))
+
+(define (make-empty-node data continues allowed-inside)
+ (make-block-node data continues allowed-inside
+ (list-queue) #f))
+
+(define (allowed-inside node line)
+ ((get-allowed-inside node) line))
+
+(define (continues node line)
+ ((get-continues node) line))
+
+(define (close-active-children node)
+ ;; Close all active children in the current node.
+ (set-active-child! node #f))
+
+(define (add-new-active-child! node child)
+ (list-queue-add-back! (node-children node) child)
+ (set-active-child! node child))
+
+(define (add-new-child node new-child-of-node continue-node line)
+ ;; Add `new-child-of-node` to `node`, and continue parsing with
+ ;; `continue-node` and `line`.
+ (add-new-active-child! node new-child-of-node)
+ (parse-line-to-node continue-node line))
+
+(define (process-active-child node line active-child)
+ ;; Act on the `active-child` with the given `line`.
+ (define (handle-xstring)
+ ;; Handle insertion of a new line when the active child is a paragraph.
+ ;;
+ ;; If the new line is the start of a block allowed in `node` then close
+ ;; `child` and add a new child. Otherwise, add to the paragraph.
+ (cond
+ ((allowed-inside node line) first-arg => (cut add-new-child node <...>))
+ (else (xstring-append! active-child line))))
+ (define (handle-child-block)
+ ;; Handle insertion of a new line when the active child is a block.
+ ;;
+ ;; Check if `line` continues `child`. If not, check if `line` is
+ ;; allowed after `child` in this node. Finally, check if `line` is
+ ;; allowed inside this node. If not, add the line as a paragraph.
+ (cond
+ ((continues active-child line) => (cut parse-line-to-node active-child <>))
+ ((allowed-inside node line) first-arg => (cut add-new-child node <...>))
+ (else (add-new-paragraph node line))))
+ (cond
+ ((xstring? active-child)
+ (handle-xstring))
+ ((block-node? active-child)
+ (handle-child-block))))
+
+(define (add-new-paragraph node line)
+ ;; Add new node as the active child to `node` with the data of `line`.
+ (add-new-active-child! node (line->xstring line)))
+
+(define (parse-line-to-node node line)
+ ;; Parse LINE as a part of NODE.
+ ;;
+ ;; If there is an active node, either add the line to the node or start a
+ ;; new node. If there is no active node, start a new node, either by
+ ;; starting a new block or by starting a new paragraph.
+ (cond
+ ((empty-line? line) (close-active-children node))
+ ((get-active-child node)
+ => (cut process-active-child node line <>))
+ ((allowed-inside node line)
+ first-arg => (cut add-new-child node <...>))
+ (else (add-new-paragraph node line))))
+
diff --git a/market/blocks.sld b/market/blocks.sld
new file mode 100644
index 0000000..cdec300
--- /dev/null
+++ b/market/blocks.sld
@@ -0,0 +1,27 @@
+#| Copyright 2024 Peter McGoron
+ |
+ | 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.
+ |#
+
+(define-library (market blocks)
+ (import (scheme base) (scheme write)
+ (srfi 26) (mcgoron srfi 61) (srfi 117)
+ (market string))
+ (cond-expand
+ (chicken (import (chicken base)))
+ (else))
+ (export make-block-node node-data node-children
+ make-empty-node block-node?
+ parse-line-to-node)
+ (include "blocks.scm"))
diff --git a/market/default.scm b/market/default.scm
new file mode 100644
index 0000000..6e7018c
--- /dev/null
+++ b/market/default.scm
@@ -0,0 +1,186 @@
+#| Copyright 2024 Peter McGoron
+ |
+ | 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.
+ |#
+
+(define (always-false line) #f)
+(define (always-true line) #t)
+
+;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;; Unordered lists and unordered list items
+;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(define (unordered-list? x)
+ (and (block-node? x) (eq? (node-data x) 'unordered-list)))
+
+(define (generate-new-unordered-list line ch num)
+ (define starts-unordered-list-item
+ (let ((prefix (list->string (cons ch
+ (unfold zero?
+ (lambda (x) #\space)
+ (cut - <> 1)
+ num)))))
+ (letrec ((starts
+ (lambda (line)
+ (define (on-success line)
+ (let ((node (make-empty-node 'unordered-list-item
+ continues-unordered-list-item
+ (default-allowed))))
+ (values node node line)))
+ (cond
+ ((line-prefix? line prefix) => on-success)
+ (else #f)))))
+ starts)))
+ (define continues-unordered-list-item
+ (let ((prefix (list->string (unfold zero?
+ (lambda (x) #\space)
+ (cut - <> 1)
+ (+ num 1)))))
+ (cut line-prefix? <> prefix)))
+ (define (on-success line)
+ (let* ((inner-list-item (make-empty-node 'unordered-list-item
+ continues-unordered-list-item
+ (default-allowed)))
+ (list-container (make-block-node 'unordered-list
+ always-false
+ starts-unordered-list-item
+ (list-queue inner-list-item)
+ inner-list-item)))
+ (values list-container inner-list-item line)))
+ (on-success line))
+
+(define (starts-unordered-list line)
+ (define (count-more-spaces line ch num)
+ (cond
+ ((eqv? (line-ref line) #\space)
+ (if (>= num 4)
+ #f
+ (count-more-spaces (line-next line) ch (+ num 1))))
+ (else (generate-new-unordered-list line ch num))))
+ (define (check-rest ch line)
+ (cond
+ ((eqv? (line-ref line) #\space)
+ (count-more-spaces (line-next line) ch 1))
+ (else #f)))
+ (cond
+ ((line-member line '(#\* #\- #\+))
+ first-arg => check-rest)
+ (else #f)))
+
+;;; ;;;;;;;;;;;;;;;;;;
+;;; Code blocks
+;;; ;;;;;;;;;;;;;;;;;;
+
+(define (code-block? x)
+ (and (block-node? x) (eq? (node-data x) 'code-block)))
+
+(define (starts-code-block line)
+ (define (continues-code-block line)
+ (or (line-prefix? line " ")
+ (line-prefix? line #\tab)))
+ (define (on-success line)
+ (let ((paragraph (string->xstring "")))
+ (values (make-block-node 'code-block
+ continues-code-block
+ always-false
+ (list-queue paragraph)
+ paragraph)
+ paragraph
+ line)))
+ (cond
+ ((continues-code-block line) => starts-code-block)
+ (else #f)))
+
+;;; ;;;;;;;;;;;;;;;;;
+;;; Block quotes
+;;; ;;;;;;;;;;;;;;;;;
+
+(define (block-quote? x)
+ (and (block-node? x) (eq? (node-data x) 'block-quote)))
+
+(define (starts-block-quote line)
+ (define (continues-block-quote line)
+ (line-prefix? line "> "))
+ (define (on-success line)
+ (let ((node (make-empty-node 'block-quote
+ continues-block-quote
+ (default-allowed))))
+ (values node node line)))
+ ;; TODO: handle `>>...`
+ (cond
+ ((continues-block-quote line) => on-success)
+ (else #f)))
+
+;;; ;;;;;;;;;;;;;;;;;;;;
+;;; ATX headings
+;;; ;;;;;;;;;;;;;;;;;;;;
+
+(define-record-type <atx-heading>
+ (make-atx-heading-data data indent)
+ atx-heading-data?
+ (data atx-heading-string)
+ (indent atx-heading-indent))
+
+(define (atx-heading? x)
+ (and (block-node? x) (atx-heading-data? (node-data x))))
+
+(define (starts-atx-heading line)
+ ;; TODO: add detecting the end of the heading
+ (define (on-success line indent)
+ (let* ((data (make-atx-heading-data (line->string line)
+ indent))
+ (node (make-empty-node data always-false always-false)))
+ (values node node (empty-line))))
+ (let detect ((line line)
+ (indent 0))
+ (cond
+ ((> indent 6) #f)
+ ((empty-line? line) #f)
+ ((char=? (line-ref line) #\#)
+ (detect (line-next line) (+ indent 1)))
+ ((= indent 0) #f)
+ ((char=? (line-ref line) #\space)
+ (on-success (line-next line) indent))
+ (else #f))))
+
+;;; ;;;;;;;;;;;;;;;;;;;;;;;
+;;; Default blocks
+;;; ;;;;;;;;;;;;;;;;;;;;;;;
+
+(define default-allowed
+ (make-parameter
+ (lambda (line)
+ (cond
+ ((starts-code-block line) first-arg => values)
+ ((starts-block-quote line) first-arg => values)
+ ((starts-atx-heading line) first-arg => values)
+ ((starts-unordered-list line) first-arg => values)
+ (else #f)))))
+
+;;; ;;;;;;;;;;;;;;;;;
+;;; The whole document
+;;; ;;;;;;;;;;;;;;;;;
+
+(define (document-node? x)
+ (and (block-node? x) (eq? (node-data x) 'document)))
+
+(define (document-node)
+ (make-empty-node 'document
+ (lambda (x) x)
+ (default-allowed)))
+
+(define (parse->document str node)
+ (let ((node (or node (document-node))))
+ (parse-line-to-node node (string->line str))
+ node))
diff --git a/market/default.sld b/market/default.sld
new file mode 100644
index 0000000..ae6eb70
--- /dev/null
+++ b/market/default.sld
@@ -0,0 +1,28 @@
+#| Copyright 2024 Peter McGoron
+ |
+ | 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.
+ |#
+
+(define-library (market default)
+ (import (scheme base) (scheme write)
+ (srfi 1) (srfi 26) (mcgoron srfi 61) (srfi 117)
+ (market blocks) (market string))
+ (cond-expand
+ (chicken (import (chicken base)))
+ (else))
+ (export unordered-list? code-block? block-quote? atx-heading?
+ default-allowed
+ document-node?
+ parse->document)
+ (include "default.scm"))
diff --git a/market/string.scm b/market/string.scm
new file mode 100644
index 0000000..f68d174
--- /dev/null
+++ b/market/string.scm
@@ -0,0 +1,127 @@
+#| Copyright 2024 Peter McGoron
+ |
+ | 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.
+ |#
+
+(define (string->generator str)
+ ;; A slightly more efficient string->generator procedure that uses
+ ;; SRFI 130 cursors.
+ (letrec* ((itr (string-cursor-start str))
+ (end (string-cursor-end str))
+ (gen (lambda ()
+ (if (= itr end)
+ (eof-object)
+ (let ((c (string-ref/cursor str itr)))
+ (set! itr (string-cursor-forward str itr 1))
+ c)))))
+ gen))
+
+;;; Lines.
+
+(define-record-type <line>
+ (make-line base-str cursor)
+ line?
+ (base-str line-str)
+ (cursor line-cursor))
+
+(cond-expand
+ (chicken
+ (set-record-printer! <line>
+ (lambda (x out)
+ (write `(line (line->string x)) out)))))
+
+(define (string->line str) (make-line str (string-cursor-start str)))
+
+(define (empty-line)
+ (string->line ""))
+
+(define-syntax with-line
+ (syntax-rules ()
+ ((with-line (line str cur) body ...)
+ (let ((str (line-str line))
+ (cur (line-cursor line)))
+ body ...))))
+
+(define (line-ref line)
+ (with-line (line str cur)
+ (if (string-cursor=? cur (string-cursor-end str))
+ (eof-object)
+ (string-ref/cursor str cur))))
+
+(define (empty-line? line) (eof-object? (line-ref line)))
+
+(define (line-next line)
+ (when (empty-line? line)
+ (error "index out of bounds" line))
+ (with-line (line str cur)
+ (make-line str (string-cursor-forward str cur 1))))
+
+(define (line->string line)
+ (with-line (line str cur)
+ (substring/cursors str cur (string-cursor-end str))))
+
+(define (coerce-to-line obj)
+ (cond
+ ((string? obj) (string->line obj))
+ ((char? obj) (string->line (string obj)))
+ (else (error "cannot convert to string" obj))))
+
+(define (line-prefix? line str)
+ (let line-prefix? ((line line)
+ (str-line (coerce-to-line str)))
+ (cond
+ ((empty-line? str-line) line)
+ ((empty-line? line) #f)
+ ((char=? (line-ref line) (line-ref str-line))
+ (line-prefix? (line-next line) (line-next str-line)))
+ (else #f))))
+
+(define (line-member line lst)
+ (if (empty-line? line)
+ #f
+ (let ((ch (line-ref line)))
+ (if (member ch lst)
+ (values ch (line-next line))
+ #f))))
+
+;;; An `<xstring>` is a mutable rope. Ropes are used because they are
+;;; easy to append to.
+
+(define-record-type <xstring>
+ (xstring rope)
+ xstring?
+ (rope xstring-rope set-xstring-rope!))
+
+(cond-expand
+ (chicken
+ (set-record-printer! <xstring>
+ (lambda (xstr out)
+ (write `(xstring ,(rope->string (xstring-rope xstr)))
+ out)))))
+
+(define (string->xstring str)
+ (xstring (rope str)))
+
+(define (xstring-append! xstring obj)
+ (let ((new-rope
+ (cond
+ ((string? obj) (rope obj))
+ ((line? obj) (rope (line->string obj)))
+ ((char? obj) (rope (string obj)))
+ (else (error "invalid object" obj)))))
+ (set-xstring-rope! xstring
+ (rope-append (xstring-rope xstring) new-rope))))
+
+(define (line->xstring line)
+ (string->xstring (line->string line)))
diff --git a/market/string.sld b/market/string.sld
new file mode 100644
index 0000000..a02ce95
--- /dev/null
+++ b/market/string.sld
@@ -0,0 +1,28 @@
+#| Copyright 2024 Peter McGoron
+ |
+ | 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.
+ |#
+
+(define-library (market string)
+ (import (scheme base) (srfi 26) (srfi 130)
+ rope)
+ (cond-expand
+ (chicken (import (scheme write) (chicken base)))
+ (else))
+ (export string->line empty-line
+ empty-line? line-ref line-next line->string line-prefix?
+ line-member
+ string->xstring
+ xstring? xstring-append! line->xstring)
+ (include "string.scm")) \ No newline at end of file
diff --git a/mcgoron/srfi/61.sld b/mcgoron/srfi/61.sld
new file mode 100644
index 0000000..0fb32b0
--- /dev/null
+++ b/mcgoron/srfi/61.sld
@@ -0,0 +1,66 @@
+#| Copyright (C) 2004 Taylor Campbell. All rights reserved.
+ |
+ | 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.
+ |#
+
+
+(define-library (mcgoron srfi 61)
+ (import (scheme base))
+ (export cond first-arg)
+ (begin
+ (define (first-arg x . rest) x))
+ (begin
+ (define-syntax cond/maybe-more
+ (syntax-rules ()
+ ((cond/maybe-more test consequent)
+ (if test
+ consequent))
+ ((cond/maybe-more test consequent clause ...)
+ (if test
+ consequent
+ (cond clause ...)))))
+ (define-syntax cond
+ (syntax-rules (=> else)
+ ((cond (else else1 else2 ...))
+ ;; The (IF #T (BEGIN ...)) wrapper ensures that there may be no
+ ;; internal definitions in the body of the clause. R5RS mandates
+ ;; this in text (by referring to each subform of the clauses as
+ ;; <expression>) but not in its reference implementation of COND,
+ ;; which just expands to (BEGIN ...) with no (IF #T ...) wrapper.
+ (if #t (begin else1 else2 ...)))
+ ((cond (test => receiver) more-clause ...)
+ (let ((T test))
+ (cond/maybe-more T
+ (receiver T)
+ more-clause ...)))
+ ((cond (generator guard => receiver) more-clause ...)
+ (call-with-values (lambda () generator)
+ (lambda T
+ (cond/maybe-more (apply guard T)
+ (apply receiver T)
+ more-clause ...))))
+ ((cond (test) more-clause ...)
+ (let ((T test))
+ (cond/maybe-more T T more-clause ...)))
+ ((cond (test body1 body2 ...) more-clause ...)
+ (cond/maybe-more test
+ (begin body1 body2 ...)
+ more-clause ...)))))) \ No newline at end of file
diff --git a/prelude.scm b/prelude.scm
new file mode 100644
index 0000000..c801175
--- /dev/null
+++ b/prelude.scm
@@ -0,0 +1,28 @@
+#| Copyright 2024 Peter McGoron
+ |
+ | 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.
+ |#
+
+(load "mcgoron/srfi/61.sld")
+(load "market/string.sld")
+(load "market/blocks.sld")
+(load "market/default.sld")
+
+(import (market default) (market blocks))
+
+(define node #f)
+(define (p s) (set! node (parse->document s node)) (display node))
+(p "> lorem")
+(p "> ipsum")
+;;; TODO: how to handle empty-line and adding EOL?