Syntax highlighting using Mediawiki formatting: Difference between revisions

julia example
m (→‎{{header|Python}}: Handle indentation in the formatter and set formatter name and aliases .)
(julia example)
Line 370:
} ''# nextChar''
 
=={{header|Julia}}==
 
''#= Keywords in Julia. Note that though two word keywords are listed, they ''
'' only work if the two words are separated by a single space character. ''
'' #= Handles nested comments such as this. =#''
''=#''
'''const''' KEYWORDS = map(
w -> Regex("^" * w * "\\W"),
sort(
[
"abstract type",
"baremodule",
"begin",
"break",
"catch",
"const",
"continue",
"do",
"else",
"elseif",
"end",
"export",
"false",
"finally",
"for",
"function",
"global",
"if",
"import",
"in",
"isa",
"let",
"local",
"macro",
"module",
"mutable struct",
"outer",
"primitive type",
"quote",
"return",
"struct",
"true",
"try",
"using",
"while",
"where",
], rev = '''true''', by = length),
) ''# reorder to largest first to find `elseif` before `else`''
""" Find the #= =# delineated comment, including nested versions """
'''function''' nestedcommentlimits(s::AbstractString, startcomment = "#=", stopcomment = "=#")
either = Regex("$startcomment|$stopcomment", "sa")
depth, startpos, stoppos = 0, 0, 0
'''for''' (i, m) '''in''' enumerate(eachmatch(either, s))
'''if''' m.match == startcomment
startpos = startpos == 0 ? m.match.offset : startpos
depth += 1
'''else'''
stoppos = max(stoppos + 1, m.match.offset + 2)
depth -= 1
'''end'''
depth <= 0 && '''break'''
'''end'''
'''return''' startpos, stoppos
'''end'''
"""
Given a string, output a string that has been modified by adding surrounding
'' or ''' bracketing for syntax highlighting of keywords and comments
"""
'''function''' partialhighlight(txt)
outtxt = Char[]
escaped = '''false'''
idx, len = 1, length(txt)
'''while''' idx <= len
'''if''' !isvalid(txt, idx)
idx += 1
'''continue'''
'''end'''
c = txt[idx]
'''if''' c == '\\'
push!(outtxt, c, txt[idx+1])
idx += 2
'''elseif''' c == '\"'
'''if''' idx < len - 2 && c == txt[idx+1] == txt[idx+2]
qlen = findfirst(r"(?<!\\)\"\"\""sa, txt[idx+3:'''end'''])
qlen == nothing &amp;&amp; error("error with terminator of quote at $idx")
app'''end'''!(outtxt, collect(txt[idx:idx+qlen.stop+2]))
idx += qlen.stop + 3
'''else'''
qlen = findfirst(r"(?<!\\)\"", txt[idx+1:'''end'''])
qlen == nothing &amp;&amp; error("error with terminator of quote at $idx")
app'''end'''!(outtxt, collect(txt[idx:idx+qlen.stop+1]))
idx += qlen.stop + 2
'''end'''
'''elseif''' c == &apos;''#'''
'''if''' idx &lt; len &amp;&amp; txt[idx+1] == &apos;=&apos;
start, stop = nestedcommentlimits(txt[idx:'''end'''])
s = replace(txt[idx:idx+stop-1], "\n" =&gt; "''\n''")
app'''end'''!(outtxt, collect("''$s''"))
idx += stop
'''else'''
newlinepos = findfirst(==(&apos;\n&apos;), txt[idx+1:'''end'''])
newlinepos == nothing &amp;&amp; error("unterminated double quote at $idx")
app'''end'''!(outtxt, collect("''$(txt[idx:idx+newlinepos-1])''"))
idx += newlinepos
'''end'''
'''elseif''' c ∈ &apos;a&apos;:&apos;z&apos;
'''for''' (j, reg) '''in''' enumerate(KEYWORDS)
m = match(reg, txt[idx:'''end'''])
'''if''' m != nothing
wlen = m.match.ncodeunits - 2
app'''end'''!(outtxt, collect("'''$(txt[idx:idx+wlen])'''"))
idx += wlen + 1
'''break'''
'''elseif''' j == lastindex(KEYWORDS)
push!(outtxt, c)
idx += 1
'''end'''
'''end'''
'''elseif''' c '''in''' [&apos;&apos;&apos;, &apos;&amp;&apos;, &apos;&lt;&apos;, &apos;&gt;&apos;]
s = c == &apos;&apos;&apos; ? "&apos;" : c == &apos;&amp;&apos; ? "&amp;" : c == &apos;&lt;&apos; ? "&lt;" : "&gt;"
app'''end'''!(outtxt, collect(s))
idx += 1
'''else'''
push!(outtxt, c)
idx += 1
'''end'''
'''end'''
'''return''' String(outtxt)
'''end'''
println(partialhighlight(read("onedrive/documents/julia programs/test1.jl", String)), "\n")
=={{header|Phix}}==
Note the utility I use for this on a day-to-day basis (pwa/p2js.exw/<Ctrl M>) must be easily over 50,000 lines of code by now...<br>
4,105

edits