-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Whitespace normalizer plugin #847
Conversation
My idea for this PR was to create a standardized way for plugins to get access to global configuration options. But I am really not sure if this is the right way to do it. We could also do it with classes or |
I've tried this on at least 30 different HTML and JS Prism highlighted snippets and have not seen any issues. Thanks! |
@CormacMcCarthy Thank you for testing it! |
I am thinking about removing the Plugin base class. It is doesn't do much and the whitespace normalizer works perfectly fine without it. |
15ad3ce
to
6168f71
Compare
Thanks Andreas, we definitely need something like this! A few remarks:
Thanks again! Don't be discouraged by the long list above, I really do appreciate the work you've put into this, and we badly need it :) |
Yes that is exactly the kind of problem I was struggling with. Currently Prism has no standard way to allow a user to change global settings for plugins. My solution involves a bit of JS, but it is quite declarative and there are sensible defaults: <script src="plugins/whitespace-normalizer/prism-whitespace-normalizer.js"></script>
<script type="text/javascript">
Prism.plugins.WhitespaceNormalizer.setOptions({
rightTrim: false,
});
</script> I agree a basic HTML API can't hurt. I'll start working on it.
I completely agree. Both of these options are disabled by default. I thought maybe there is a use for someone.
The defaults are set in the constructor: Prism.plugins.WhitespaceNormalizer = new WhitespaceNormalizer({
removeTrailingWhitespace: true,
removeExtraIndent: true,
leftTrim: true,
rightTrim: true,
//tabsToSpaces: 4,
//spacesToTabs: 4,
});
The names have to match the names of the methods of the WhitespaceNormalizer object. I used the Java naming conventions for methods. But I can use shorter names that are a better fit for
I'll change it. |
An HTML API doesn't have to be on every element that is highlighted. Prism solves that problem by inheriting the |
And yes, Java conventions result in huge names, to the point that Java developers are often made fun of for it :P Thankfully, native JS APIs are moving away from this (e.g. things like |
That's a great idea! I'll work on it. It shouldn't be too hard to implement. Why didn't I think of that 😄
Yeah I know. I am usually the one who makes fun of Java developers for that. Nevertheless I like camel case for method names. The shorter the better of course. |
6168f71
to
c7e2160
Compare
I have added another plugin to this PR. The plugin parses inheritable data-* attributes and classes and stores the result in the environment object |
c7e2160
to
fe2c1e4
Compare
Sorry to spring more work on you, but can we split the two PRs? |
I've just found an old PR #825. It's a Trim plugin, that does exactly the same thing as the Whitespace Normalizer. Argh, I should have checked the list of existing PRs before starting this plugin... |
It's not your fault, there are a lot of PRs… |
I'd be happy to, but the Normalize-Whitespace plugin depends on the Parse-Settings plugin. If I separate them into two PRs the Normalize-Whitespace plugin wouldn't work and couldn't be tested properly. PR #825 has some nice ideas. We should also consider it. The object-oriented approach of my Normalize-Whitespace plugin makes it a bit bulky compared to the simpler Trim plugin. |
I could do most of the stuff in a more functional programming style by using |
Yes, code size/readability/elegance are more important, unless of course there is a noticeable performance impact on average use cases. But avoiding |
Yes I think I am guilty of premature optimization 😄. I don't think that there is a noticeable performance impact. I will clean it up and submit another PR for the |
Heh, I know what you mean, I was too in the past. Then I realized that these differences shown in jsperf didn't matter for 99% of the projects, 99% of the time. However keeping the code small does, mainly so that humans can process and skim it quickly. |
This plugin normalizes whitespace in code blocks. It can perform various operations. The user can configure them through the plugin object located at Prism.plugins.NormalizeWhitespace. Prism.plugins.NormalizeWhitespace.setDefaults({ 'remove-trailing': true, 'remove-indent': true, 'left-trim': true, 'right-trim': true, /*'indent': 2, 'remove-initial-line-feed': false, 'tabs-to-spaces': 4, 'spaces-to-tabs': 4*/ }); The plugin can be disabled for certain code blocks, by adding the class "no-whitespace-normalization" to it. There is also a HTML-interface using the parse-settings plugin.
fe2c1e4
to
e86ec01
Compare
I've removed the hard dependency on the |
This patch adds support for automatic line breaks after a specific number of characters. The default is 80 characters, but it can be set to any number.
I've added support for automatic line breaks to this PR. |
Merged, thanks! Sorry I haven't reviewed the parse settings plugin yet, I'll get to it! |
This plugin normalizes whitespace in code blocks. It can perform
various operations. The user can configure them through the plugin
object located at Prism.plugins.WhitespaceNormalizer.
The plugin can be disabled for certain code blocks, by adding the
class "no-whitespace-normalization" to it.