Syntax highlighting using Mediawiki formatting: Difference between revisions

(Add Python)
Line 370:
} ''# nextChar''
 
=={{header|Phix}}==
Note the utility I use for this on a day-to-day basis must be easily over 50,000 lines of code by now...
''--''
''-- demo\rosetta\syntax_highlight.exw''
''-- =================================''
''--''
'''sequence''' cl = '''command_line'''()
'''string''' pgm = '''substitute'''('''get_text'''(cl[$]),"\r\n","\n")
''-- or(/for javascript compatibility) specify constant pgm = """...""" ''
'''constant''' qqq = `""`&`"`,
keywords = {`and`,`assert`,`bool`,`command_line`,`constant`,`do`,`else`,`end`,`find`,
`for`,`function`,`get_text`,`if`,`in`,`integer`,`length`,`match`,`not`,`procedure`,
`puts`,`return`,`sequence`,`string`,`substitute`,`then`,`wait_key`,`while`},
htmlify = {"'&<>",{`apos`,`amp`,`lt`,`gt`}}
'''integer''' i = 1, l = '''length'''(pgm), word_start = 0
'''string''' out = " "
'''procedure''' spacenl('''string''' s)
'''for''' ch '''in''' s '''do'''
out &amp;= ch
'''if''' ch='\n' '''then''' out &amp;= ' ' '''end''' '''if'''
'''end''' '''for'''
'''end''' '''procedure'''
'''function''' do_string('''integer''' i, ni, l, '''string''' stype)
'''assert'''(ni&gt;0,"%d quoted string not closed",{stype})
ni += l
spacenl(pgm[i..ni])
'''return''' ni
'''end''' '''function'''
'''while''' i&lt;=l '''do'''
'''integer''' ch = pgm[i]
'''if''' (ch&gt;='a' '''and''' ch&lt;='z') or ch='_' '''then'''
'''if''' '''not''' word_start '''then''' word_start := i '''end''' '''if'''
'''else'''
'''if''' word_start '''then'''
'''string''' one_word = pgm[word_start..i-1]
'''bool''' is_key = '''find'''(one_word,keywords)
'''if''' is_key '''then''' out &amp;= `'''` '''end''' '''if'''
out &amp;= one_word
'''if''' is_key '''then''' out &amp;= `'''` '''end''' '''if'''
word_start = 0
'''end''' '''if'''
'''if''' ch='-' '''and''' i&lt;l '''and''' pgm[i+1]='-' '''then'''
''-- nb: does not handle --/* style comments''
'''integer''' line_comment = i
'''while''' i&lt;l '''and''' pgm[i+1]!='\n' '''do''' i += 1 '''end''' '''while'''
''-- if pgm[i]='\n' then i -= 1 end if''
out &amp;= `''` &amp; pgm[line_comment..i] &amp; `''`
elsif ch='/' '''and''' i&lt;l '''and''' pgm[i+1]='*' '''then'''
'''integer''' block_comment = i
i = '''match'''(`*/`,pgm,i+2)+1
'''assert'''(i&gt;1,"missing closing block comment")
spacenl(pgm[block_comment..i])
elsif ch='"' '''then'''
'''if''' i+1&lt;l '''and''' pgm[i..i+2]=qqq '''then'''
i = do_string(i,'''match'''(qqq,pgm,i+3),2,"triple")
'''else'''
i = do_string(i,'''find'''('"',pgm,i+1),0,"double")
'''end''' '''if'''
elsif '''find'''(ch,"`'") '''then'''
'''string''' stype = iff(ch='`'?"backtick":"single")
i = do_string(i,'''find'''(ch,pgm,i+1),0,stype)
'''else'''
'''integer''' k = '''find'''(ch,htmlify[1])
'''if''' k '''then'''
out &amp;= '&' &amp; htmlify[2][k] &amp; ';'
'''else'''
out &amp;= ch
'''if''' ch='\n' '''then''' out &amp;= ' ' '''end''' '''if'''
'''end''' '''if'''
'''end''' '''if'''
'''end''' '''if'''
i += 1
'''end''' '''while'''
'''puts'''(1,out)
{} = '''wait_key'''()
 
 
=={{header|Python}}==
7,795

edits