Paper markdown (#30873)
* why it doesn't work * list thing * oops * fixes list. maybe * Less regex * wip * Done I think. I think * return your functions * Replace $10 by $a
This commit is contained in:
+150
-25
@@ -414,34 +414,159 @@ GLOBAL_LIST_INIT(binary, list("0","1"))
|
||||
end = temp
|
||||
return end
|
||||
|
||||
|
||||
/proc/parsepencode(t, mob/user=null, signfont=SIGNFONT)
|
||||
if(length(t) < 1) //No input means nothing needs to be parsed
|
||||
/proc/parsemarkdown_basic_step1(t, limited=FALSE)
|
||||
if(length(t) <= 0)
|
||||
return
|
||||
|
||||
t = replacetext(t, "\[center\]", "<center>")
|
||||
t = replacetext(t, "\[/center\]", "</center>")
|
||||
t = replacetext(t, "\[br\]", "<BR>")
|
||||
t = replacetext(t, "\[b\]", "<B>")
|
||||
t = replacetext(t, "\[/b\]", "</B>")
|
||||
t = replacetext(t, "\[i\]", "<I>")
|
||||
t = replacetext(t, "\[/i\]", "</I>")
|
||||
t = replacetext(t, "\[u\]", "<U>")
|
||||
t = replacetext(t, "\[/u\]", "</U>")
|
||||
t = replacetext(t, "\[large\]", "<font size=\"4\">")
|
||||
t = replacetext(t, "\[/large\]", "</font>")
|
||||
if(user)
|
||||
t = replacetext(t, "\[sign\]", "<font face=\"[signfont]\"><i>[user.real_name]</i></font>")
|
||||
else
|
||||
t = replacetext(t, "\[sign\]", "")
|
||||
t = replacetext(t, "\[field\]", "<span class=\"paper_field\"></span>")
|
||||
// This parses markdown with no custom rules
|
||||
|
||||
t = replacetext(t, "\[*\]", "<li>")
|
||||
t = replacetext(t, "\[hr\]", "<HR>")
|
||||
t = replacetext(t, "\[small\]", "<font size = \"1\">")
|
||||
t = replacetext(t, "\[/small\]", "</font>")
|
||||
t = replacetext(t, "\[list\]", "<ul>")
|
||||
t = replacetext(t, "\[/list\]", "</ul>")
|
||||
// Escape backslashed
|
||||
|
||||
t = replacetext(t, "$", "$-")
|
||||
t = replacetext(t, "\\\\", "$1")
|
||||
t = replacetext(t, "\\**", "$2")
|
||||
t = replacetext(t, "\\*", "$3")
|
||||
t = replacetext(t, "\\__", "$4")
|
||||
t = replacetext(t, "\\_", "$5")
|
||||
t = replacetext(t, "\\^", "$6")
|
||||
t = replacetext(t, "\\((", "$7")
|
||||
t = replacetext(t, "\\))", "$8")
|
||||
t = replacetext(t, "\\|", "$9")
|
||||
t = replacetext(t, "\\%", "$0")
|
||||
|
||||
// Escape single characters that will be used
|
||||
|
||||
t = replacetext(t, "!", "$a")
|
||||
|
||||
// Parse hr and small
|
||||
|
||||
if(!limited)
|
||||
t = replacetext(t, "((", "<font size=\"1\">")
|
||||
t = replacetext(t, "))", "</font>")
|
||||
t = replacetext(t, regex("^-{3,}$", "gm"), "<hr>")
|
||||
t = replacetext(t, regex("^\\(-{3,})$", "gm"), "$1")
|
||||
|
||||
// Parse lists
|
||||
|
||||
var/list/tlist = splittext(t, "\n")
|
||||
var/tlistlen = tlist.len
|
||||
var/listlevel = -1
|
||||
var/singlespace = -1 // if 0, double spaces are used before asterisks, if 1, single are
|
||||
for(var/i = 1, i <= tlistlen, i++)
|
||||
var/line = tlist[i]
|
||||
var/count_asterisk = length(replacetext(line, regex("\[^\\*\]+", "g"), ""))
|
||||
if(count_asterisk % 2 == 1 && findtext(line, regex("^\\s*\\*", "g"))) // there is an extra asterisk in the beggining
|
||||
|
||||
var/count_w = length(replacetext(line, regex("^( *)\\*.*$", "g"), "$1")) // whitespace before asterisk
|
||||
line = replacetext(line, regex("^ *(\\*.*)$", "g"), "$1")
|
||||
|
||||
if(singlespace == -1 && count_w == 2)
|
||||
if(listlevel == 0)
|
||||
singlespace = 0
|
||||
else
|
||||
singlespace = 1
|
||||
|
||||
if(singlespace == 0)
|
||||
count_w = count_w % 2 ? round(count_w / 2 + 0.25) : count_w / 2
|
||||
|
||||
line = replacetext(line, regex("\\*", ""), "<li>")
|
||||
while(listlevel < count_w)
|
||||
line = "<ul>" + line
|
||||
listlevel++
|
||||
while(listlevel > count_w)
|
||||
line = "</ul>" + line
|
||||
listlevel--
|
||||
|
||||
else while(listlevel >= 0)
|
||||
line = "</ul>" + line
|
||||
listlevel--
|
||||
|
||||
tlist[i] = line
|
||||
// end for
|
||||
|
||||
t = tlist[1]
|
||||
for(var/i = 2, i <= tlistlen, i++)
|
||||
t += "\n" + tlist[i]
|
||||
|
||||
while(listlevel >= 0)
|
||||
t += "</ul>"
|
||||
listlevel--
|
||||
|
||||
else
|
||||
t = replacetext(t, "((", "")
|
||||
t = replacetext(t, "))", "")
|
||||
|
||||
// Parse headers
|
||||
|
||||
t = replacetext(t, regex("^#(?!#) ?(.+)$", "gm"), "<h2>$1</h2>")
|
||||
t = replacetext(t, regex("^##(?!#) ?(.+)$", "gm"), "<h3>$1</h3>")
|
||||
t = replacetext(t, regex("^###(?!#) ?(.+)$", "gm"), "<h4>$1</h4>")
|
||||
t = replacetext(t, regex("^#### ?(.+)$", "gm"), "<h5>$1</h5>")
|
||||
|
||||
// Parse most rules
|
||||
|
||||
t = replacetext(t, regex("\\*(\[^\\*\]*)\\*", "g"), "<i>$1</i>")
|
||||
t = replacetext(t, regex("_(\[^_\]*)_", "g"), "<i>$1</i>")
|
||||
t = replacetext(t, "<i></i>", "!")
|
||||
t = replacetext(t, "</i><i>", "!")
|
||||
t = replacetext(t, regex("\\!(\[^\\!\]+)\\!", "g"), "<b>$1</b>")
|
||||
t = replacetext(t, regex("\\^(\[^\\^\]+)\\^", "g"), "<font size=\"4\">$1</font>")
|
||||
t = replacetext(t, regex("\\|(\[^\\|\]+)\\|", "g"), "<center>$1</center>")
|
||||
t = replacetext(t, "!", "</i><i>")
|
||||
|
||||
return t
|
||||
|
||||
/proc/parsemarkdown_basic_step2(t)
|
||||
if(length(t) <= 0)
|
||||
return
|
||||
|
||||
// Restore the single characters used
|
||||
|
||||
t = replacetext(t, "$a", "!")
|
||||
|
||||
// Redo the escaping
|
||||
|
||||
t = replacetext(t, "$1", "\\")
|
||||
t = replacetext(t, "$2", "**")
|
||||
t = replacetext(t, "$3", "*")
|
||||
t = replacetext(t, "$4", "__")
|
||||
t = replacetext(t, "$5", "_")
|
||||
t = replacetext(t, "$6", "^")
|
||||
t = replacetext(t, "$7", "((")
|
||||
t = replacetext(t, "$8", "))")
|
||||
t = replacetext(t, "$9", "|")
|
||||
t = replacetext(t, "$0", "%")
|
||||
t = replacetext(t, "$-", "$")
|
||||
|
||||
return t
|
||||
|
||||
/proc/parsemarkdown_basic(t, limited=FALSE)
|
||||
t = parsemarkdown_basic_step1(t, limited)
|
||||
t = parsemarkdown_basic_step2(t)
|
||||
return t
|
||||
|
||||
/proc/parsemarkdown(t, mob/user=null, limited=FALSE)
|
||||
if(length(t) <= 0)
|
||||
return
|
||||
|
||||
// Premanage whitespace
|
||||
|
||||
t = replacetext(t, regex("\[^\\S\\r\\n \]", "g"), " ")
|
||||
|
||||
t = parsemarkdown_basic_step1(t)
|
||||
|
||||
t = replacetext(t, regex("%s(?:ign)?(?=\\s|$)", "igm"), user ? "<font face=\"[SIGNFONT]\"><i>[user.real_name]</i></font>" : "<span class=\"paper_field\"></span>")
|
||||
t = replacetext(t, regex("%f(?:ield)?(?=\\s|$)", "igm"), "<span class=\"paper_field\"></span>")
|
||||
|
||||
t = parsemarkdown_basic_step2(t)
|
||||
|
||||
// Manage whitespace
|
||||
|
||||
t = replacetext(t, regex("(?:\\r\\n?|\\n)", "g"), "<br>")
|
||||
|
||||
t = replacetext(t, " ", " ")
|
||||
|
||||
// Done
|
||||
|
||||
return t
|
||||
|
||||
|
||||
@@ -305,7 +305,7 @@ GLOBAL_LIST_EMPTY(allCasters)
|
||||
dat+="Creating new Feed Message..."
|
||||
dat+="<HR><B><A href='?src=\ref[src];set_channel_receiving=1'>Receiving Channel</A>:</B> [channel_name]<BR>"
|
||||
dat+="<B>Message Author:</B> <FONT COLOR='green'>[scanned_user]</FONT><BR>"
|
||||
dat+="<B><A href='?src=\ref[src];set_new_message=1'>Message Body</A>:</B> <BR><font face=\"[PEN_FONT]\">[parsepencode(msg, user, SIGNFONT)]</font><BR>"
|
||||
dat+="<B><A href='?src=\ref[src];set_new_message=1'>Message Body</A>:</B> <BR><font face=\"[PEN_FONT]\">[parsemarkdown(msg, user)]</font><BR>"
|
||||
dat+="<B><A href='?src=\ref[src];set_attachment=1'>Attach Photo</A>:</B> [(photo ? "Photo Attached" : "No Photo")]</BR>"
|
||||
dat+="<B><A href='?src=\ref[src];set_comment=1'>Comments [allow_comments ? "Enabled" : "Disabled"]</A></B><BR>"
|
||||
dat+="<BR><A href='?src=\ref[src];submit_new_message=1'>Submit</A><BR><BR><A href='?src=\ref[src];setScreen=[0]'>Cancel</A><BR>"
|
||||
@@ -555,7 +555,7 @@ GLOBAL_LIST_EMPTY(allCasters)
|
||||
if(msg =="" || msg=="\[REDACTED\]" || scanned_user == "Unknown" || channel_name == "" )
|
||||
screen=6
|
||||
else
|
||||
GLOB.news_network.SubmitArticle("<font face=\"[PEN_FONT]\">[parsepencode(msg, usr, SIGNFONT)]</font>", scanned_user, channel_name, photo, 0, allow_comments)
|
||||
GLOB.news_network.SubmitArticle("<font face=\"[PEN_FONT]\">[parsemarkdown(msg, usr)]</font>", scanned_user, channel_name, photo, 0, allow_comments)
|
||||
SSblackbox.inc("newscaster_stories",1)
|
||||
screen=4
|
||||
msg = ""
|
||||
|
||||
@@ -433,7 +433,7 @@ GLOBAL_LIST_EMPTY(PDAs)
|
||||
if (in_range(src, U) && loc == U)
|
||||
if (mode == 1 && n)
|
||||
note = n
|
||||
notehtml = parsepencode(n, U, SIGNFONT)
|
||||
notehtml = parsemarkdown(n, U)
|
||||
notescanned = 0
|
||||
else
|
||||
U << browse(null, "window=pda")
|
||||
|
||||
@@ -170,12 +170,19 @@
|
||||
t = replacetext(t, "\[td\]", "<td>")
|
||||
t = replacetext(t, "\[cell\]", "<td>")
|
||||
t = replacetext(t, "\[tab\]", " ")
|
||||
|
||||
t = parsemarkdown_basic(t)
|
||||
|
||||
return t
|
||||
|
||||
/datum/computer_file/program/filemanager/proc/prepare_printjob(t) // Additional stuff to parse if we want to print it and make a happy Head of Personnel. Forms FTW.
|
||||
t = replacetext(t, "\[field\]", "<span class=\"paper_field\"></span>")
|
||||
t = replacetext(t, "\[sign\]", "<span class=\"paper_field\"></span>")
|
||||
|
||||
t = parse_tags(t)
|
||||
|
||||
t = replacetext(t, regex("(?:%s(?:ign)|%f(?:ield))(?=\\s|$)", "ig"), "<span class=\"paper_field\"></span>")
|
||||
|
||||
return t
|
||||
|
||||
/datum/computer_file/program/filemanager/ui_data(mob/user)
|
||||
|
||||
@@ -187,47 +187,15 @@
|
||||
if(length(t) < 1) //No input means nothing needs to be parsed
|
||||
return
|
||||
|
||||
// t = copytext(sanitize(t),1,MAX_MESSAGE_LEN)
|
||||
|
||||
t = replacetext(t, "\[center\]", "<center>")
|
||||
t = replacetext(t, "\[/center\]", "</center>")
|
||||
t = replacetext(t, "\[br\]", "<BR>")
|
||||
t = replacetext(t, "\n", "<BR>")
|
||||
t = replacetext(t, "\[b\]", "<B>")
|
||||
t = replacetext(t, "\[/b\]", "</B>")
|
||||
t = replacetext(t, "\[i\]", "<I>")
|
||||
t = replacetext(t, "\[/i\]", "</I>")
|
||||
t = replacetext(t, "\[u\]", "<U>")
|
||||
t = replacetext(t, "\[/u\]", "</U>")
|
||||
t = replacetext(t, "\[large\]", "<font size=\"4\">")
|
||||
t = replacetext(t, "\[/large\]", "</font>")
|
||||
t = replacetext(t, "\[sign\]", "<font face=\"[SIGNFONT]\"><i>[user.real_name]</i></font>")
|
||||
t = replacetext(t, "\[field\]", "<span class=\"paper_field\"></span>")
|
||||
t = replacetext(t, "\[tab\]", " ")
|
||||
t = parsemarkdown(t, user, iscrayon)
|
||||
|
||||
if(!iscrayon)
|
||||
t = replacetext(t, "\[*\]", "<li>")
|
||||
t = replacetext(t, "\[hr\]", "<HR>")
|
||||
t = replacetext(t, "\[small\]", "<font size = \"1\">")
|
||||
t = replacetext(t, "\[/small\]", "</font>")
|
||||
t = replacetext(t, "\[list\]", "<ul>")
|
||||
t = replacetext(t, "\[/list\]", "</ul>")
|
||||
|
||||
t = "<font face=\"[P.font]\" color=[P.colour]>[t]</font>"
|
||||
else // If it is a crayon, and he still tries to use these, make them empty!
|
||||
else
|
||||
var/obj/item/toy/crayon/C = P
|
||||
t = replacetext(t, "\[*\]", "")
|
||||
t = replacetext(t, "\[hr\]", "")
|
||||
t = replacetext(t, "\[small\]", "")
|
||||
t = replacetext(t, "\[/small\]", "")
|
||||
t = replacetext(t, "\[list\]", "")
|
||||
t = replacetext(t, "\[/list\]", "")
|
||||
|
||||
t = "<font face=\"[CRAYON_FONT]\" color=[C.paint_color]><b>[t]</b></font>"
|
||||
|
||||
// t = replacetext(t, "#", "") // Junk converted to nothing!
|
||||
|
||||
//Count the fields
|
||||
// Count the fields
|
||||
var/laststart = 1
|
||||
while(1)
|
||||
var/i = findtext(t, "<span class=\"paper_field\">", laststart)
|
||||
@@ -253,22 +221,23 @@
|
||||
/obj/item/paper/proc/openhelp(mob/user)
|
||||
user << browse({"<HTML><HEAD><TITLE>Paper Help</TITLE></HEAD>
|
||||
<BODY>
|
||||
You can use backslash (\\) to escape special characters.<br>
|
||||
<br>
|
||||
<b><center>Crayon&Pen commands</center></b><br>
|
||||
<br>
|
||||
\[br\] : Creates a linebreak.<br>
|
||||
\[center\] - \[/center\] : Centers the text.<br>
|
||||
\[b\] - \[/b\] : Makes the text <b>bold</b>.<br>
|
||||
\[i\] - \[/i\] : Makes the text <i>italic</i>.<br>
|
||||
\[u\] - \[/u\] : Makes the text <u>underlined</u>.<br>
|
||||
\[large\] - \[/large\] : Increases the <font size = \"4\">size</font> of the text.<br>
|
||||
\[sign\] : Inserts a signature of your name in a foolproof way.<br>
|
||||
\[field\] : Inserts an invisible field which lets you start type from there. Useful for forms.<br>
|
||||
# text : Defines a header.<br>
|
||||
|text| : Centers the text.<br>
|
||||
**text** : Makes the text <b>bold</b>.<br>
|
||||
*text* : Makes the text <i>italic</i>.<br>
|
||||
^text^ : Increases the <font size = \"4\">size</font> of the text.<br>
|
||||
%s : Inserts a signature of your name in a foolproof way.<br>
|
||||
%f : Inserts an invisible field which lets you start type from there. Useful for forms.<br>
|
||||
<br>
|
||||
<b><center>Pen exclusive commands</center></b><br>
|
||||
\[small\] - \[/small\] : Decreases the <font size = \"1\">size</font> of the text.<br>
|
||||
\[list\] - \[/list\] : A list.<br>
|
||||
\[*\] : A dot used for lists.<br>
|
||||
\[hr\] : Adds a horizontal rule.
|
||||
((text)) : Decreases the <font size = \"1\">size</font> of the text.<br>
|
||||
* item : An unordered list item.<br>
|
||||
* item: An unordered list child item.<br>
|
||||
--- : Adds a horizontal rule.
|
||||
</BODY></HTML>"}, "window=paper_help")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user