git-touch / highlight.dart Goto Github PK
View Code? Open in Web Editor NEWSyntax highlighting for Dart and Flutter
Home Page: https://git-touch.github.io/highlight/
License: MIT License
Syntax highlighting for Dart and Flutter
Home Page: https://git-touch.github.io/highlight/
License: MIT License
PS C:\Users\tusha\code\flutter\highlight\flutter_highlight\example> flutter run
Launching lib\main.dart on ONEPLUS A5000 in debug mode...
[!] Your app isn't using AndroidX.
To avoid potential build failures, you can quickly migrate your app by following the steps on https://goo.gl/CP92wY
Is there a hightlight for html?
Is there anyway I use the syntax highlighter for editable textfields?
Only a widget can be accessed for now. Access to a TextEditingController
based class that style it's text could be added. It would allow any TextField
to highlight syntax.
RichText
to SelectableText.rich
@override
Widget build(BuildContext context) {
/// ....
return Container(
color: theme[_rootKey]?.backgroundColor ?? _defaultBackgroundColor,
padding: padding,
child: SelectableText.rich(
TextSpan(
style: _textStyle,
children:
_convert(highlight.parse(source, language: language).nodes!),
),
),
);
}
Can I be able to load multiple language like dart,PHP, vue
example_map.dart is missing from flutter_highlight_gallery/lib/generated
I found that the syntax I needed was not highlighted in any way. I decided to check the rest of the languages and found more recognition errors.
How to make a HighlightView
widget's text selectable?
Are there any plans to migrate these packages to non-nullable-by-default?
For this input:
class MyClass {
private string str = "abc";
}
we get the correct result:
Make this string literal multiline:
class MyClass {
private string str = """
abc
""";
}
and nothing is parsed:
Note that language is also null
although java
is explicitly passed.
This codepen shows that the original highlightjs can handle multiline string literals: https://codepen.io/alexey-inkin/pen/abYYVOj
import 'package:highlight/highlight_core.dart';
import 'package:highlight/languages/java.dart';
void main() {
highlight.registerLanguage('java', java);
const text = '''
class MyClass {
private string str = "abc";
}
''';
final result = highlight.parse(text, language: 'java');
print(result.nodes);
const textMultiline = '''
class MyClass {
private string str = """
abc
""";
}
''';
final resultMultiline = highlight.parse(textMultiline, language: 'java');
print(resultMultiline.nodes);
}
[Instance of 'Node', Instance of 'Node', Instance of 'Node', Instance of 'Node', Instance of 'Node', Instance of 'Node', Instance of 'Node']
[Instance of 'Node']
I wanted to add Godots GDScript to the supported languages. For testing purposes I just copied the python implementation and replaced some keywords and it seems to work fine enough.
As the language files have stated // GENERATED CODE - DO NOT MODIFY BY HAND
at the top I came to thinking that those language files are generated somehow. As I couldn't find documenation in how to generate another language I stopped work there.
Are those files somehow generated by converting the available languages from highlight.js? If so there is the 3rd party package highlightjs-gdscript which could be used.
If this is not the way to go then how to add languages to highlight.dart?
Please add line by line parsing ... Below is currently my workaround (I saved the latest continuation and pass it back on the next line parse). The block comments work correctly. But I'm not sure if this is the right way to do this.
index fdab3dc..6cf7c6e 100644
--- a/highlight/lib/src/highlight.dart
+++ b/highlight/lib/src/highlight.dart
@@ -251,7 +251,9 @@ class Highlight {
/// [autoDetect]: The default value is `false`. Pass `true` to enable language auto detection.
/// Notice that **this may cause performance issue** because it will try to parse source with
/// all registered languages and use the most relevant one.
- Result parse(String source, {String? language, bool autoDetection = false}) {
+
+ Mode? lastContinuation;
+ Result parse(String source, {String? language, bool autoDetection = false, Mode? continuation}) {
if (language == null) {
if (autoDetection) {
return _parseAuto(source);
@@ -259,7 +261,7 @@ class Highlight {
throw ArgumentError.notNull('language');
}
}
- return _parse(source, language: language);
+ return _parse(source, language: language, continuation: continuation);
}
Result _parse(
@@ -369,6 +371,7 @@ class Highlight {
if (explicit) {
continuations[top!.subLanguage!.first] = result.top;
}
+
return _buildSpan(result.language, result.nodes, noPrefix: true);
}
@@ -378,6 +381,7 @@ class Highlight {
? _processSubLanguage()!
: _processKeywords(),
currentChildren);
+ lastContinuation = top;
mode_buffer = '';
}
@@ -404,6 +408,7 @@ class Highlight {
}
}
_startNewMode(new_mode);
+
return new_mode.returnBegin == true ? 0 : lexeme.length;
}
import 'package:markdown_widget/config/highlight_themes.dart' as theme;
Widget buildMarkdown() => MarkdownWidget(
data: data,
styleConfig: StyleConfig(
preConfig: PreConfig(
language: 'java',
theme: theme.a11yLightTheme
)
),
);
_convert(hi.highlight.parse(source, language: null, autoDetection: true)
flutter: FormatException: Invalid number (at character 1)
^
flutter: FormatException: Invalid radix-10 number (at character 1)
=
^
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.17.1, on Mac OS X 10.15.3 19D76, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 11.1)
[✓] Android Studio (version 3.6)
[✓] VS Code (version 1.43.1)
[✓] Connected device (1 available)
this needs autoDetection: true
https://github.com/git-touch/highlight/blob/master/flutter_highlight/lib/flutter_highlight.dart#L96
It looks like the language.dart files are all generated somehow. What is the process for adding new langauges?
Hi, I am trying to implement javascript syntax highlight for TextField with a custom TextEditingController here.
I noticed that the library does not work if there are any unenclosed strings except the last line.
For example, It does not work in:
It leads to an unpleasant result when typing:
Could you help me look into this? Thank you!
This project seems abandoned, so we forked it and did the following:
Language
class in addition to Mode
to better mimic the original HighlightJS classes.The new package is published as:
We intend to maintain our fork.
See the migration guide: https://github.com/akvelon/dart-highlighting/blob/main/doc/migrating.md
(Posting this here as there seems to be no other way to inform the users of the package who are in need of changes.)
For this input:
class MyClass {
}
we get the correct result:
Add a number sign:
class MyClass {
}#
and nothing is parsed:
Note that language is also null
although java
is explicitly passed.
import 'package:highlight/highlight_core.dart';
import 'package:highlight/languages/java.dart';
void main() {
highlight.registerLanguage('java', java);
const text = '''
class MyClass {
}
''';
final result = highlight.parse(text, language: 'java');
print(result.nodes);
const textNumber = '''
class MyClass {
}#
''';
final resultNumber = highlight.parse(textNumber, language: 'java');
print(resultNumber.nodes);
}
[Instance of 'Node', Instance of 'Node', Instance of 'Node']
[Instance of 'Node']
I think you could replace this
final _languages = {}.cast<String, Mode>()
with this
final _languages = <String, Mode>{};
Hi,
I had a same Java code wrapped with <pre>
tag, like this:
List<Integer> arr = new List<Integer>;
when I display it in flutter with highlight, it will be trimed to:
List arr = new List;
Is there a method to resovle it?
Thanks!
I understand that this is a port from https://highlightjs.org
I guess the port was automated to some extent, because language files contain the comment:
// GENERATED CODE - DO NOT MODIFY BY HAND
This package now has quite a number of bugs that are not reproducible with the original Highlight.js, like
Also I can see no progress on older issues.
Can you please share the process of this porting so anyone can fast-forward the changes in Highlight.js? These then can be contributed here or implemented in a fork. This would solve most of the problems.
Or share any hints if the port was manual.
Hi,
Is it possible to use different code files in Orgro for better viewing?
Where can elaborate steps be seen?
Thanks
Nehal
There are no html Highlights
Not that everytime we need the whole container, can you add method just to get the List of TextSpans?
What is the current maintenance status of this project? There are many PRs pending, some of them being easy-merges. Tagging author: @pd4d10. Maybe it is time to find new maintainers?
Support for line numbers in files?
As far as I understand, double quotes ("
) should be escaped with "
in the generated HTML.
<
, >
and &
are correctly escaped.
import 'package:highlight/highlight.dart' show highlight;
main() {
var source = '''main() {
print("Hello <> World!");
}
''';
var result = highlight.parse(source, language: 'dart');
var html = result.toHtml();
print(html); // HTML string
}
Actual output is:
<span class="hljs-built_in">print</span>(<span class="hljs-string">"Hello <> World!"</span>);
Expected output is:
<span class="hljs-built_in">print</span>(<span class="hljs-string">"Hello <> World!"</span>);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.