forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7e9103
commit 05e826a
Showing
8 changed files
with
451 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
.code .comment { | ||
color: #998; | ||
font-style: italic; | ||
} | ||
|
||
|
||
|
||
.string { | ||
color: #DD0A73; | ||
} | ||
|
||
.numeric, | ||
.variable { | ||
color: #00A69B; | ||
} | ||
.keyword, | ||
.primitive, | ||
.modifier { | ||
color: #333; | ||
font-weight: bold; | ||
} | ||
|
||
.type, | ||
.class { | ||
color: #0079a5; | ||
font-weight: bold; | ||
} | ||
|
||
|
||
|
||
|
||
.parameter { | ||
color: inherit; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import React, {Component} from "react"; | ||
|
||
import { | ||
EuiButton, | ||
EuiCheckboxGroup, | ||
EuiFieldText, | ||
EuiForm, | ||
EuiFormRow, | ||
EuiTextArea, | ||
EuiSpacer, | ||
EuiButton, | ||
EuiSwitch, | ||
EuiPanel, | ||
EuiCode, | ||
EuiCodeBlock, | ||
} from '@elastic/eui'; | ||
|
||
import "./code.css" | ||
|
||
interface State { | ||
path: string | ||
content: string, | ||
html? : string | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
export default class Code extends Component<any, State> { | ||
constructor(props: any) { | ||
super(props); | ||
this.state = { | ||
path: "HelloWorld.java", | ||
content: ` | ||
comments */ | ||
import System.out; | ||
class HelloWorld { | ||
public static void main(String[] args){ | ||
// some comments | ||
int x = 5; | ||
System.out.println("hello world"); | ||
} | ||
} | ||
`, | ||
} | ||
} | ||
|
||
highlight() { | ||
const {httpClient} = this.props; | ||
httpClient.post(`../api/castro/highlight/${this.state.path}`, {content: this.state.content}).then(response => { | ||
this.setState({html: response.data}) | ||
}) | ||
} | ||
|
||
|
||
render() { | ||
return ( | ||
<div> | ||
<EuiForm> | ||
<EuiFormRow | ||
label="Text field" | ||
helpText="Your file name." | ||
> | ||
<EuiFieldText name="first" placeholder={"HelloWorld.java"} | ||
value={this.state.path} | ||
onChange={(e) => this.setState({path: e.target.value})} | ||
/> | ||
</EuiFormRow> | ||
<EuiFormRow | ||
label="Source" | ||
fullWidth | ||
helpText="Put your source content here" | ||
> | ||
<EuiTextArea | ||
fullWidth | ||
placeholder="class HelloWorld {}" | ||
value={this.state.content} | ||
onChange={(e) => this.setState({content: e.target.value})} | ||
/> | ||
</EuiFormRow> | ||
<EuiButton type="submit" size="s" fill onClick={() => this.highlight()}> | ||
Highlight | ||
</EuiButton> | ||
</EuiForm> | ||
<EuiSpacer size="xl"/> | ||
<EuiPanel paddingSize="l" hasShadow> | ||
{ this.state.html && | ||
<EuiCode dangerouslySetInnerHTML={{ __html: this.state.html }} /> | ||
} | ||
</EuiPanel> | ||
<EuiPanel paddingSize="l" hasShadow> | ||
<EuiCodeBlock language="java"> | ||
{this.state.content} | ||
</EuiCodeBlock> | ||
</EuiPanel> | ||
</div> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const Highlights = require('highlights'); | ||
const highlighter = new Highlights(); | ||
const Selector = require('first-mate-select-grammar'); | ||
|
||
highlighter.loadGrammarsSync(); | ||
|
||
const selector = Selector(); | ||
|
||
interface CodeLine extends Array<Token> { | ||
} | ||
|
||
interface Token { | ||
value: string | ||
scopes: string[] | ||
range?: Range | ||
} | ||
|
||
interface Range { | ||
start: number // start pos in line | ||
end: number | ||
pos?: number // position in file | ||
} | ||
|
||
|
||
export function tokenizeLines(filePath: string, fileContents: string): CodeLine[] { | ||
const grammar = selector.selectGrammar(highlighter.registry, filePath, fileContents); | ||
if (grammar) { | ||
return grammar.tokenizeLines(fileContents); | ||
} else { | ||
return []; | ||
} | ||
} | ||
|
||
export function computeRanges(lines: CodeLine[]) { | ||
let pos = 0; | ||
for (let line of lines) { | ||
let start = 0; | ||
|
||
for (let token of line) { | ||
let len = token.value.length; | ||
token.range = { | ||
start, | ||
end: start + len, | ||
pos | ||
}; | ||
start += len; | ||
pos += len; | ||
} | ||
pos += 1; // line break | ||
} | ||
} | ||
|
||
export function render(lines: CodeLine[]) : string{ | ||
let output = "<pre class='code'>"; | ||
for(let line of lines) { | ||
output += "<div class='code-line'>"; | ||
for(let token of line){ | ||
let lastScope = token.scopes[token.scopes.length - 1]; | ||
const clazz = lastScope.replace(/\./g, " "); | ||
output += `<span class="${clazz}">${highlighter.escapeString(token.value)}</span>` | ||
} | ||
output += "\n</div>"; | ||
} | ||
|
||
return output + "</pre>" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.