Giter Site home page Giter Site logo

Comments (11)

korny avatar korny commented on June 30, 2024

a Ruby terminal application on top of CodeRay

Awesome :-)

From your examples, I guess that you looked at the Terminal encoder. It is not a good reference of how token kinds are used. Instead, I suggest you look at styles/alpha.rb which should contain all important token kind nestings.

regexp.function is obsolete and not used anywere, to my knowledge.

(in :string) Why does escape sub-attribute not set a color for escape sequences?

That's because escape sequences like \t are marked as :char, not :escape. :escape is used by the Ruby scanner to mark the # character in inline variables like "#$0".

The Terminal encode should highlight :char inside of strings. I change this, thank you for spotting it!

What is a string :modifier?

PHP, Python, C, and even SQL use modifiers for strings to mark them as binary, Unicode, wchar_t or something else.

Why does self sub-attribute set a color value for …

Can you explain more? What output do you get, and which one did you expect?

from coderay.

kyrylo avatar kyrylo commented on June 30, 2024

From your examples, I guess that you looked at the Terminal encoder. It is not a good reference of how token kinds are used. Instead, I suggest you look at styles/alpha.rb which should contain all important token kind nestings.

I did look at the Terminal encoder. Although styles/alpha.rb contains not only Ruby tokens, thanks for the tip.

That's because escape sequences like \t are marked as :char, not :escape. :escape is used by the Ruby scanner to mark the # character in inline variables like "#$0".

:escape colors an octothorpe sign only in the following situations: "#@ivar", "#$gvar". But it doesn't color the sign in interpolations: "age: #{age}". For that case we use :self token. Am I understanding you correctly?

Can you explain more? What output do you get, and which one did you expect?

For example, I have the following string: "#{n} times\n". In order to color #{} and \n I use :self token, while the rest of the string is colored via :content token. I didn't know about :char, so it looks like I misuse these tokens a bit.

However, when you remove a color value of :content, the times section (in string #{n} times\n") gets colored to a color value of :self. That is a bit confusing, so I don't really understand what :self token does.

from coderay.

korny avatar korny commented on June 30, 2024

Code interpolations inside of strings should be an :inline group, and the #{ and } parts would be :inline_delimiter tokens inside that group.

For that case we use :self token. Am I understanding you correctly?

There is no :self token. It's just the way the Terminal encoder specifies the color of the un-nested token (eg. :string). Maybe this is causing the problem you describe. Do you need to use the Terminal encoder? If not, I would suggest taking the :self method as an anti-pattern for now. Token nestings should instead be understood like CSS classes on nested SPANs, since HTML is the intended output type of CodeRay.

I'm sorry that there is no description of the token kind semantic right now ^_^ Maybe I can find the time to write an article about that. Feel free to ask if you run into more unexpected behavior.

from coderay.

kyrylo avatar kyrylo commented on June 30, 2024

Do you need to use the Terminal encoder?

Yes, the "connection" between my program and CodeRay is established via CodeRay::Encoders::Terminal::TOKEN_COLORS constant.

I'm sorry that there is no description of the token kind semantic right now ^_^ Maybe I can find the time to write an article about that. Feel free to ask if you run into more unexpected behavior.

It would be great. Thank you for the answers. Currently, I have no questions left. Thanks!

from coderay.

kyrylo avatar kyrylo commented on June 30, 2024

It looks like inline has a precedence over the inline_delimiter. When I set inline and inline_delimiter both, the latter merely isn't taken into account.

Example:

Given string: "#{foo} bar"

inline : red
inline_delimiter : blue

So instead of having a blue color for inline_delimiter (#{ and }) and a red color for foo, I have #{foo} colored to red. So a blue color of inline_delimiter is overwritten by the value of inline. When I set inline_delimiter only, I get #{ and } colored to blue and foo colored to default color of a terminal.

My question: how to separate apples from oranges? I want to get inline_delimiter working with inline.

from coderay.

korny avatar korny commented on June 30, 2024

The Terminal encoder is not able to work with tokens nested deeper than 2 levels, as far as I know (it's not my implementation, but Rob Aldred's = @raldred, I believe).

If you want to have this functionality, you have to fix/enhance the Terminal encoder. I recommend branching off or copying it.

The CodeRay library contains no code to interpret all possible token nestings; rather, that's left to the browser in the form of HTML+CSS. Since the terminal has no concept of colors applied to nested spans of text, you would need to recreate quite a bit of browser layout logic.

Or you find something that converts HTML+CSS to ANSI colored text.

from coderay.

kyrylo avatar kyrylo commented on June 30, 2024

The Terminal encoder is not able to work with tokens nested deeper than 2 levels, as far as I know

But they are not nested: https://github.com/rubychan/coderay/blob/master/lib/coderay/styles/alpha.rb#L85-86

from coderay.

korny avatar korny commented on June 30, 2024

They are, in the HTML code, and also in the token structure. The Debug encoder is helpful here:

puts CodeRay.scan('"#{foo} bar"', :ruby).debug
#=> string<delimiter(")inline<inline_delimiter(#{)ident(foo)inline_delimiter(})>content( bar)delimiter(")>

string<…> is a token group of the kind :string (with its content between the angle brackets), and delimiter(…) is a token of kind :delimiter (with the text between the parentheses). So, the structure of the token output is:

string<
  delimiter(")
  inline<
    inline_delimiter(#{)
    ident(foo)
    inline_delimiter(})
  >
  content( bar)
  delimiter(")
>

from coderay.

kyrylo avatar kyrylo commented on June 30, 2024

OK, thank you :)

I'll keep this issue open, if you don't mind (maybe someone wants to fix the nesting problems).

from coderay.

nathany avatar nathany commented on June 30, 2024

The current implementation doesn't handle more than one nesting of token groups. We need to immitate some CSS behavior.

From Redmine: http://odd-eyed-code.org/issues/145

from coderay.

korny avatar korny commented on June 30, 2024

Changed the title subtly.

from coderay.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.