[REBOL] eText
From: al:bri:xtra at: 14-Nov-2000 23:56
Here's some preliminary code on my eText script (it's nearly right, just a
few little problems I haven't fixed yet):
[
Rebol [
Name: 'eText
Title: "eText"
File: %"eText.r"
Author: "Andrew Martin"
eMail: [Al--Bri--xtra--co--nz]
Date: 14/November/2000
Home: http://members.nbci.com/AndrewMartin/Rebol/eText/eText.r
Version: 1.0.0
Purpose: {Takes a string of text and transforms it into HTML dialect.}
Usage: [
write %Document.html eText read %Document.txt
]
]
do %../units/ascii.r
do %../units/flow.r
eText!: make object! [
Replacements: func [Document [string!]] [
foreach [Target Replacement] [
"&" "&"
"<" "<"
">" ">"
"---" "—" ; Em dash
"--" "–" ; En dash
"-TM" "™" ; Trademark
"(C)" "©" ; Copyright
"(R)" "®" ; Registered trademark
"..." "…" ; Ellipsis
"1/4" "¼"
"1/2" "½"
"3/4" "¾"
" x " " × " ; Multiply sign
" / " " ÷ " ; Division sign
][
replace/all Document Target Replacement
]
Document
]
Printable: ASCII/Printable
Dialect: make block! 0
Text: none
InitialParagraph: true
Fragment: false
Title: [
copy Text some Printable "^/" (
append Dialect either Fragment [
reduce [
'h1 Text
]
][
reduce [
'title Text
'body
'h1 Text
]
]
)
]
Heading: [
2 "^/" copy Text some Printable 2 "^/" (
InitialParagraph: true
repend Dialect [
'h2 Text
]
)
]
Paragraph: [
"^/" copy Text some Printable opt "^/" (
repend Dialect [
either InitialParagraph [
InitialParagraph: false
'ip
][
'p
]
trim/lines Text
]
)
]
CodeLines: make block! 0
CodeLine: [
2 "^-" any "^-" copy Text some Printable "^/" (
append CodeLines join Text "^/"
)
]
Code: [
"^/" some CodeLine "^/" (
repend Dialect [
'BlockQuote 'Pre CodeLines
]
CodeLines: make block! 0
InitialParagraph: true
)
]
ListItems: make block! 0
ListItem: [
some "^-" copy Text some Printable "^/" (
append Text ListItems
)
]
ListType: none
BulletListItem: [
"*" ListItem (ListType: none)
]
CapitalLetterListItem: [
"A" ListItem (ListType: "A")
]
LowercaseLetterListItem: [
"a" ListItem (ListType: "a")
]
CapitalRomanListItem: [
"I" ListItem (ListType: "I")
]
LowercaseRomanListItem: [
"i" ListItem (ListType: "i")
]
ArabicListItem: [
["1" | "0"] ListItem (ListType: "1")
]
List: [
"^/" some [
BulletListItem
| CapitalLetterListItem
| LowercaseLetterListItem
| CapitalRomanListItem
| LowercaseRomanListItem
| ArabicListItem
] "^/" (
append Dialect either none? ListType [
reduce ['list ListItems]
][
reduce ['list/type ListItems ListType]
]
ListItems: make block! 0
InitialParagraph: true
)
]
InitialTableRow: true
TableText: difference ASCII/Printable charset "|^/"
TableCells: make block! 0
TableCell: [
copy Text some TableText ["|" | "^/"] (
repend TableCells [
either InitialTableRow ['TH]['TD] trim Text
]
)
]
TableRows: make block! 0
TableRow: [
"|" some TableCell (
repend TableRows [
'TR TableCells
]
InitialTableRow: false
TableCells: make block! 0
)
]
Table: [
"^/" some TableRow "^/" (
repend Dialect [
'Table TableRows
]
TableRows: make block! 0
InitialParagraph: true
InitialTableRow: true
)
]
HorizontalRule: [
any "^/" 3 "-" any "-" opt "^/" (append Dialect 'hr)
]
set 'eText func [
{Takes a string of text and transforms it into HTML dialect.}
Document [string!]
/Fragment {This text is only a fragment of a larger HTML page.}
][
self/Fragment: Fragment
parse/case/all Replacements flow Document [
any [
Title
any [
Heading | HorizontalRule | Table | Code | List |
Paragraph
]
]
copy Text
to end (print copy/part Text 100)
]
Dialect
]
]
]
What does it do? It converts plain ascii text with "natural" formatting,
like this (Emma from the Gutenberg project):
Emma, by Jane Austen
VOLUME I
CHAPTER I
Emma Woodhouse, handsome, clever, and rich, with a comfortable home
and happy disposition, seemed to unite some of the best blessings
of existence; and had lived nearly twenty-one years in the world
with very little to distress or vex her.
She was the youngest of the two daughters of a most affectionate,
indulgent father; and had, in consequence of her sister's marriage,
been mistress of his house from a very early period. Her mother
had died too long ago for her to have more than an indistinct
remembrance of her caresses; and her place had been supplied
by an excellent woman as governess, who had fallen little short
of a mother in affection.
To nice HTML code like this:
<html>
<head>
<title>Emma, by Jane Austen</title>
</head>
<body>
<h1>Emma, by Jane Austen</h1>
<h2>VOLUME I</h2>
<h2>CHAPTER I</h2>
Emma Woodhouse, handsome, clever, and rich, with a comfortable home and
happy disposition, seemed to unite some of the best blessings of existence;
and had lived nearly twenty-one years in the world with very little to
distress or vex her.
<br>     
She was the youngest of the two daughters of a most affectionate, indulgent
father; and had, in consequence of her sister's marriage, been mistress of
his house from a very early period. Her mother had died too long ago for her
to have more than an indistinct remembrance of her caresses; and her place
had been supplied by an excellent woman as governess, who had fallen little
short of a mother in affection.
There's also facility for tables, lists and putting in code examples, and so
on.
Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/