Git Product home page Git Product logo

Comments (10)

 avatar commented on August 13, 2024

in nodes.py create_node function, fixing the indentation for the following gets me a bit further:

    if stripped_line == PYGMENTS_FILTER:
        return PygmentsFilterNode(haml_line)

test code:

from hamlpy import hamlpy


haml = ":highlight\nprint 'hi'"
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)

print result
<div class="highlight"><pre><span class="p">:</span><span class="n">highlight</span>
</pre></div>
print 'hi'

At the moment I'm a bit lost trying to grep the rest of the code to figure out why PygmentsFilterNode never has any internal_nodes, it only seems to have access to the highlight tag itself and not the content. I'm sure I'm missing something obvious.

from hamlpy.

 avatar commented on August 13, 2024

Yep, my test code wasn't indented properly.

I think this works now...

from hamlpy import hamlpy


haml = ":highlight\n    print 'hi'"
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)

print result
class PygmentsFilterNode(FilterNode):
    def render(self):
        text = "".join((''.join((node.spaces, node.haml,'\n')) for node in self.internal_nodes))
        output = highlight(text, guess_lexer(text), HtmlFormatter())
        return output
<div class="highlight"><pre>    <span class="n">print</span> <span class="s">&#39;hi&#39;</span>
</pre></div>

I'll play with it some more and submit a pull request tomorrow if I haven't overlooked anything obvious.

from hamlpy.

 avatar commented on August 13, 2024

atm I can't figure out how to reconstruct the nodes so that the correct number of blank lines are preserved within the pre block, anyone have any hints?

Alternatively I would have thought there'd be a way to access the raw haml block prior to being stripped of whitespace but I can't find it.

from hamlpy.

bcoughlan avatar bcoughlan commented on August 13, 2024

Is the problem that it's not leaving line breaks after each line? Do you have any HAML example that demonstrates the problem

from hamlpy.

 avatar commented on August 13, 2024

It's not preserving any blank lines within a pre. From my reading of the haml docs, they'd use :preserve or ~ to keep whitespace and line breaks within a tag but neither seem to be present in hamlpy.

%pre
    from flask import Flask

    app = Flask(__name__)

    @app.route("/")
    def hello():
        return "Hello World!"


    if __name__ == "__main__":
        app.run()

%p Paragraph
<pre>
    from flask import Flask
    app = Flask(__name__)
    @app.route("/")
    def hello():
        return "Hello World!"
    if __name__ == "__main__":
        app.run()
</pre>
<p>hi there</p>

from hamlpy.

bcoughlan avatar bcoughlan commented on August 13, 2024

Hey, just got a chance to look at this.

Blank lines are removed in nodes.py's create_node() with the lines:

    if not stripped_line:
        return None

Essentially this causes them not to be added to the list of nodes, which is why blank lines are removed from output.

You were saying that haml has :preserve and ~. Can you think of any reason why hamlpy should not preserve by default (I can't)? Preserving by default would be as simple as changing the above to

    if not stripped_line:
        return HamlNode(haml_line)

and fixing the four tests that fail due to the empty lines.

from hamlpy.

 avatar commented on August 13, 2024

Preserve by default seems logical to me, can't think of a case where you'd want the opposite.

from hamlpy.

bcoughlan avatar bcoughlan commented on August 13, 2024

Had a proper look at this. This is trickier than it looks, if you have

%pre
  Foo
(Blank line, no spaces)
  Bar

If the blank line has no indentation HamlPy thinks it's the end of the <pre> tag.

There are a few ways to fix this, but I'll have to do some more thinking to decide on the best way.

from hamlpy.

artcz avatar artcz commented on August 13, 2024

I've created a simple patch, that adds a pipe support (much like shpaml)

here's the commit:

https://github.com/czartur/HamlPy/commit/707d3c34870e19e0db0e872a5de474ebfee64ab4

and it works exactly like described here:
https://github.com/czartur/HamlPy/blob/master/reference.md#dropping-leading-whitespace

from hamlpy.

bcoughlan avatar bcoughlan commented on August 13, 2024

I've been too busy lately to have a proper look at this issue.

@czartur It seems a bit cumbersome to do it this way. Since the purpose of haml is to be easier than html, I wouldn't like to see something added that is more difficult to do than HTML.

There may be a simpler hack in hamlpy.py (where multi-line text is merged into one line) where we could append \n's to a HamlNode.

Another possibility would be to get the indentation of a blank line to match the indentation of the previous line, which would solve the problem I mentioned previously. In practice this would require some refactoring in create_node so that the previous node is passed in.

from hamlpy.

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.