ultradebug 发表于 2022-8-17 14:39:33

代码高亮Demo(富文本模式)

HTML, XML
<!DOCTYPE html>
<title>Title</title>

<style>body {width: 500px;}</style>

<script type="application/javascript">
function $init() {return true;}
</script>

<body>
<p checked class="title" id='title'>Title</p>
<!-- here goes the rest of the page -->
</body>
Bash
#!/bin/bash

###### CONFIG
ACCEPTED_HOSTS="/root/.hag_accepted.conf"
BE_VERBOSE=false

if [ "$UID" -ne 0 ]
then
echo "Superuser rights required"
exit 2
fi

genApacheConf(){
echo -e "# Host ${HOME_DIR}$1/$2 :"
}

echo '"quoted"' | tr -d \" > text.txt
C++
#include <iostream>

int main(int argc, char *argv[]) {

/* An annoying "Hello World" example */
for (auto i = 0; i < 0xFFFF; i++)
    cout << "Hello, World!" << endl;

char c = '\n';
unordered_map <string, vector<string> > m;
m["key"] = "\\\\"; // this is an error

return -2e3 + 12l;
}
C#
using System.IO.Compression;

#pragma warning disable 414, 3021

namespace MyApplication
{
   
    class Program : IInterface
    {
      public static List<int> JustDoIt(int count)
      {
            Span<int> numbers = stackalloc int;
            Console.WriteLine($"Hello {Name}!");
            return new List<int>(new int[] { 1, 2, 3 })
      }
    }
}
CSS
@font-face {
font-family: Chunkfive; src: url('Chunkfive.otf');
}

body, .usertext {
color: #F0F0F0; background: #600;
font-family: Chunkfive, sans;
--heading-1: 30px/32px Helvetica, sans-serif;
}

@import url(print.css);
@media print {
a::after {
    content: attr(href)
}
}
Markdown
# hello world

you can write text (http://example.com) inline or .

* one _thing_ has *em*phasis
* two __things__ are **bold**

: http://example.com

---

hello world
===========

<this_is inline="xml"></this_is>

> markdown is so cool

    so are code segments

1. one thing (yeah!)
2. two thing `i can write code`, and `more` wipee!

Diff
Index: languages/ini.js
===================================================================
--- languages/ini.js    (revision 199)
+++ languages/ini.js    (revision 200)
@@ -1,8 +1,7 @@
hljs.LANGUAGES.ini =
{
   case_insensitive: true,
-defaultMode:
-{
+defaultMode: {
   contains: ['comment', 'title', 'setting'],
   illegal: '[^\\s]'
   },

*** /path/to/original timestamp
--- /path/to/new      timestamp
***************
*** 1,3 ****
--- 1,9 ----
+ This is an important
+ notice! It should
+ therefore be located at
+ the beginning of this
+ document!

! compress the size of the
! changes.

It is important to spell
Ruby
# The Greeter class
class Greeter
def initialize(name)
    @name = name.capitalize
end

def salute
    puts "Hello #{@name}!"
end
end

g = Greeter.new("world")
g.salute
Go
package main

import "fmt"

func main() {
    ch := make(chan float64)
    ch <- 1.0e10    // magic number
    x, ok := <- ch
    defer fmt.Println(`exitting now\`)
    go println(len("hello world!"))
    return
}
GraphQL
# Graphql detection template

mutation Hero($episode: Episode, $withFriends: Boolean!) {
hero(episode: $episode) {
    name
    friends @include(if: $withFriends) {
      name
      ... on Droid {
      primaryFunction
      }
      ... on Human {
      height
      }
    }
}
}

query HeroNameAndFriends {
hero {
    name
    friends {
      name
    }
}
}

fragment comparisonFields on Character {
name
appearsIn
friends {
    name
}
}

input StoryLikeSubscribeInput {
storyId: string
clientSubscriptionId: string
}
TOML, also INI
; boilerplate

name = "some_name"
authors = ["Author"]
description = "This is \
a description"

[]
name = ${NAME}
default = True
auto = no
counter = 1_000
Java
/**
* @author John Smith <john.smith@example.com>
*/
package l2f.gameserver.model;

public abstract strictfp class L2Char extends L2Object {
public static final Short ERROR = 0x0001;

public void moveTo(int x, int y, int z) {
    _ai = null;
    log("Should not be called");
    if (1 > 5) { // wtf!?
      return;
    }
}
}
JavaScript
function $initHighlight(block, cls) {
try {
    if (cls.search(/\bno\-highlight\b/) != -1)
      return process(block, true, 0x0F) +
             ` class="${cls}"`;
} catch (e) {
    /* handle exception */
}
for (var i = 0 / 2; i < classes.length; i++) {
    if (checkCondition(classes) === undefined)
      console.log('undefined');
}

return (
    <div>
      <web-component>{block}</web-component>
    </div>
)
}

export$initHighlight;
JSON
[
{
    "title": "apples",
    "count": ,
    "description": {"text": "...", "sensitive": false}
},
{
    "title": "oranges",
    "count": ,
    "description": {"text": "...", "sensitive": false}
}
]
Kotlin
import kotlinx.serialization.Serializable
import kotlin.random.Random

interface Building

@Serializable
class House(
    private val rooms: Int? = 3,
    val name: String = "Palace"
) : Building {
    var residents: Int = 4
      get() {
            println("Current residents: $field")
            return field
      }

    fun burn(evacuation: (people: Int) -> Boolean) {
      rooms ?: return
      if (evacuation((0..residents).random()))
            residents = 0
    }
}

fun main() {
    val house = House(name = "Skyscraper 1")
    house.burn {
      Random.nextBoolean()
    }
}
Less
@import "fruits";

@rhythm: 1.5em;

@media screen and (min-resolution: 2dppx) {
    body {font-size: 125%}
}

section > .foo + #bar:hover {
    margin:   @rhythm 0 0 @rhythm;
    padding:    calc(5% + 20px);
    background: #f00ba7 url(http://placehold.alpha-centauri/42.png) no-repeat;
    background-image: linear-gradient(-135deg, wheat, fuchsia) !important ;
    background-blend-mode: multiply;
}

@font-face {
    font-family: /* ? */ 'Omega';
    src: url('../fonts/omega-webfont.woff?v=2.0.2');
}

.icon-baz::before {
    display:   inline-block;
    font-family: "Omega", Alpha, sans-serif;
    content:   "\f085";
    color:       rgba(98, 76 /* or 54 */, 231, .75);
}
Lua
--[[
Simple signal/slot implementation
]]
local signal_mt = {
    __index = {
      register = table.insert
    }
}
function signal_mt.__index:emit(... --[[ Comment in params ]])
    for _, slot in ipairs(self) do
      slot(self, ...)
    end
end
local function create_signal()
    return setmetatable({}, signal_mt)
end

-- Signal test
local signal = create_signal()
signal:register(function(signal, ...)
    print(...)
end)
signal:emit('Answer to Life, the Universe, and Everything:', 42)

--[==[ [=[ [[
Nested ]]
multi-line ]=]
comment ]==]
[==[ Nested
[=[ multi-line
[[ string
]] ]=] ]==]
Makefile
# Makefile

BUILDDIR      = _build
EXTRAS       ?= $(BUILDDIR)/extras

.PHONY: main clean

main:
      @echo "Building main facility..."
      build_main $(BUILDDIR)

clean:
      rm -rf $(BUILDDIR)/*
Perl
# loads object
sub load
{
my $flds = $c->db_load($id,@_) || do {
    Carp::carp "Can`t load (class: $c, id: $id): '$!'"; return undef
};
my $o = $c->_perl_new();
$id12 = $id / 24 / 3600;
$o->{'ID'} = $id12 + 123;
#$o->{'SHCUT'} = $flds->{'SHCUT'};
my $p = $o->props;
my $vt;
$string =~ m/^sought_text$/;
$items = split //, 'abc';
$string //= "bar";
for my $key (keys %$p)
{
    if(${$vt.'::property'}) {
      $o->{$key . '_real'} = $flds->{$key};
      tie $o->{$key}, 'CMSBuilder::Property', $o, $key;
    }
}
$o->save if delete $o->{'_save_after_load'};

# GH-117
my $g = glob("/usr/bin/*");

return $o;
}

__DATA__
@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
__END__

=head1 NAME
POD till the end of file
Objective-C
#import <UIKit/UIKit.h>
#import "Dependency.h"

@protocol WorldDataSource
@optional
- (NSString*)worldName;
@required
- (BOOL)allowsToLive;
@end

@property (nonatomic, readonly) NSString *title;
- (IBAction) show;
@end
PHP
require_once 'Zend/Uri/Http.php';

namespace Location\Web;

interface Factory
{
    static function _factory();
}

abstract class URI extends BaseURI implements Factory
{
    abstract function test();

    public static $st1 = 1;
    const ME = "Yo";
    var $list = NULL;
    private $var;

    /**
   * Returns a URI
   *
   * @return URI
   */
    static public function _factory($stats = array(), $uri = 'http')
    {
      echo __METHOD__;
      $uri = explode(':', $uri, 0b10);
      $schemeSpecific = isset($uri) ? $uri : '';
      $desc = 'Multi
line description';

      // Security check
      if (!ctype_alnum($scheme)) {
            throw new Zend_Uri_Exception('Illegal scheme');
      }

      $this->var = 0 - self::$st;
      $this->list = list(Array("1"=> 2, 2=>self::ME, 3 => \Location\Web\URI::class));

      return [
            'uri'   => $uri,
            'value' => null,
      ];
    }
}

match ($key) {
    1 => 'Integer 1',
    '1' => 'String 1',
    true => 'Bool true',
    [] => 'Empty array',
    => 'Array ',
};

enum Foo: string {
    case Test = 'test';
}

echo URI::ME . URI::$st1;

__halt_compiler () ; datahere
datahere
datahere */
datahere
PHP Template
<html>
</html>
<body>
<?php
# Here echo command is used to print
echo "Hello, world!";
?>
</body>
Python
@requires_authorization(roles=["ADMIN"])
def somefunc(param1='', param2=0):
    r'''A docstring'''
    if param1 > param2: # interesting
      print 'Gre\'ater'
    return (param2 - param1 + 1 + 0b10l) or None

class SomeClass:
    pass

>>> message = '''interpreter
... prompt'''
Python REPL
>>> v = "foo = 42"
>>> v
"foo = 42"
>>> print(v)
foo = 42
>>> print(repr(v).rstrip('"'))
"foo = 42
>>> print(repr(v).lstrip('"'))
foo = 42"

>>> """
... abc
... """
>>> def test():
...   pass
R
require(stats)

#' Compute different averages
#'
#' @param x \code{numeric} vector of sample data
#' @param type \code{character} vector of length 1 specifying the average type
#' @return \code{centre} returns the sample average according to the chosen method.
#' @examples
#' centre(rcauchy(10), "mean")
#' @export
centre <- function(x, type) {
switch(type,
         mean = mean(x),
         median = median(x),
         trimmed = mean(x, trim = .1))
}
x <- rcauchy(10)
centre(x, "mean")

library(ggplot2)

models <- tibble::tribble(
~model_name,    ~ formula,
"length-width", Sepal.Length ~ Petal.Width + Petal.Length,
"interaction",Sepal.Length ~ Petal.Width * Petal.Length
)

iris %>%
nest_by(Species) %>%
left_join(models, by = character()) %>%
rowwise(Species, model_name) %>%
mutate(model = list(lm(formula, data = data))) %>%
summarise(broom::glance(model))
#> `summarise()` regrouping output by 'Species', 'model_name' (override with `.groups` argument)
#> # A tibble: 6 x 13
#> # Groups:   Species, model_name
#>   Species model_name r.squared adj.r.squared sigma statisticp.value    df
#>   <fct>   <chr>          <dbl>         <dbl> <dbl>   <dbl>    <dbl> <int>
#> 1 setosalength-wi…   0.112      0.0739 0.339      2.96 6.18e- 2   3
#> 2 setosainteracti…   0.133      0.0760 0.339      2.34 8.54e- 2   4
#> 3 versic… length-wi…   0.574      0.5560.344   31.71.92e- 9   3
#> 4 versic… interacti…   0.577      0.5490.347   20.91.11e- 8   4
#> 5 virgin… length-wi…   0.747      0.7360.327   69.39.50e-15   3
#> 6 virgin… interacti…   0.757      0.7410.323   47.83.54e-14   4
#> # … with 5 more variables: logLik <dbl>, AIC <dbl>, BIC <dbl>, deviance <dbl>,
#> #   df.residual <int>
Rust
#
pub enum State {
    Start,
    Transient,
    Closed,
}

impl From<&'a str> for State {
    fn from(s: &'a str) -> Self {
      match s {
            "start" => State::Start,
            "closed" => State::Closed,
            _ => unreachable!(),
      }
    }
}
SCSS
@import "compass/reset";

// variables
$colorGreen: #008000;
$colorGreenDark: darken($colorGreen, 10);

@mixin container {
    max-width: 980px;
}

// mixins with parameters
@mixin button($color:green) {
    @if ($color == green) {
      background-color: #008000;
    }
    @else if ($color == red) {
      background-color: #B22222;
    }
}

button {
    @include button(red);
}

div,
.navbar,
#header,
input {
    font-family: "Helvetica Neue", Arial, sans-serif;
    width: auto;
    margin: 0 auto;
    display: block;
}

.row-12 > {
    border-left: 1px solid #B5C583;
}

// nested definitions
ul {
    width: 100%;
    padding: {
      left: 5px; right: 5px;
    }
li {
      float: left; margin-right: 10px;
      .home {
          background: url('http://placehold.it/20') scroll no-repeat 0 0;
    }
}
}

.banner {
    @extend .container;
}

a {
color: $colorGreen;
&:hover { color: $colorGreenDark; }
&:visited { color: #c458cb; }
}

@for $i from 1 through 5 {
    .span#{$i} {
      width: 20px*$i;
    }
}

@mixin mobile {
@media screen and (max-width : 600px) {
    @content;
}
}
Shell Session
$ echo $EDITOR
vim
$ git checkout main
Switched to branch 'main'
Your branch is up-to-date with 'origin/main'.
$ git push
Everything up-to-date
$ echo 'All
> done!'
All
done!
SQL
CREATE TABLE "topic" (
    "id" integer NOT NULL PRIMARY KEY,
    "forum_id" integer NOT NULL,
    "subject" varchar(255) NOT NULL
);
ALTER TABLE "topic"
ADD CONSTRAINT forum_id FOREIGN KEY ("forum_id")
REFERENCES "forum" ("id");

-- Initials
insert into "topic" ("forum_id", "subject")
values (2, 'D''artagnian');
Swift
import Foundation

@objc class Person: Entity {
var name: String!
var age:Int!

init(name: String, age: Int) {
    /* /* ... */ */
}

// Return a descriptive string for this person
func description(offset: Int = 0) -> String {
    return "\(name) is \(age + offset) years old"
}
}
YAML
---
# comment
string_1: "Bar"
string_2: 'bar'
string_3: bar
inline_keys_ignored: sompath/name/file.jpg
keywords_in_yaml:
- true
- false
- TRUE
- FALSE
- 21
- 21.0
- !!str 123
"quoted_key": &foobar
bar: foo
foo:
"foo": bar

reference: *foobar

multiline_1: |
Multiline
String
multiline_2: >
Multiline
String
multiline_3: "
Multiline string
"

ansible_variables: "foo {{variable}}"

array_nested:
- a
- b: 1
c: 2
- b
- comment
TypeScript
class MyClass {
public static myValue: string;
constructor(init: string) {
    this.myValue = init;
}
}
import fs = require("fs");
module MyModule {
export interface MyInterface extends Other {
    myProperty: any;
}
}
declare magicNumber number;
myArray.forEach(() => { }); // fat arrow syntax
Visual Basic .NET
Imports System
Imports System.IO
#Const DEBUG = True

Namespace Highlighter.Test
''' <summary>This is an example class.</summary>
Public Class Program
    Protected Shared hello As Integer = 3
    Private Const ABC As Boolean = False

#Region "Code"
    ' Cheers!
    <STAThread()> _
    Public Shared Sub Main(ByVal args() As String, ParamArray arr As Object) Handles Form1.Click
      On Error Resume Next
      If ABC Then
      While ABC : Console.WriteLine() : End While
      For i As Long = 0 To 1000 Step 123
          Try
            System.Windows.Forms.MessageBox.Show(CInt("1").ToString())
          Catch ex As Exception       ' What are you doing? Well...
            Dim exp = CType(ex, IOException)
            REM ORZ
            Return
          End Try
      Next
      Else
      Dim l As New System.Collections.List<String>()
      SyncLock l
          If TypeOf l Is Decimal And l IsNot Nothing Then
            RemoveHandler button1.Paint, delegate
          End If
          Dim d = New System.Threading.Thread(AddressOf ThreadProc)
          Dim a = New Action(Sub(x, y) x + y)
          Static u = From x As String In l Select x.Substring(2, 4) Where x.Length > 0
      End SyncLock
      Do : Laugh() : Loop Until hello = 4
      End If
    End Sub
#End Region
End Class
End Namespace
WebAssembly
(module
(import "js" "memory" (memory 1))
(import "js" "table" (table 1 funcref))
(type $void_to_i32 (func (result i32)))
(func (export "doIt") (result i32)
   i32.const 0
   i32.const 42
   i32.store;; store 42 at address 0
   i32.const 0
   call_indirect (type $void_to_i32))
)
页: [1]
查看完整版本: 代码高亮Demo(富文本模式)