Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Call notepad commands with Python (XML Tools)

fb16
2012-05-20
2012-11-14
  • fb16
    fb16
    2012-05-20

    Hello,

    I created a file "Encode HTML Text Entities.py" in "C:\Program Files (x86)\Notepad++\plugins\PythonScript\scripts\encodeHTMLentities\" with the following lines. What I want to achieve is the execution the two commands over a selected chunk of text, in order to, first, encode all the entities, and then decode only the entities which are part of HTML tags.

    notepad.runMenuCommand("HTML Tag", "Encode HTML entities")
    notepad.runPluginCommand("XML Tools", "Convert selection text to XML (&lt;&gt; => <>)")
    

    But unfortunately, only the first command (HTML Tag) is executed while it seems like the second one is not found.

    Any hints will be appreciated. Thank you!

    Regards

     
  • "&" in menu commands is escaped, so it should be

    notepad.runPluginCommand("XML Tools", "Convert selection text to XML (&&lt;&&gt; => <>)")
    

    Took me a moment, that one! :)

    Also, be aware that that command needs a selection, I don't know what "Encode HTML entities" does with the selection, but if scraps it you may need to remember the offsets and reapply the selection after running the first command.  Of course the text will be that much longer, so you may need to work around that (perhaps using scintilla targets, but I expect they'd be wiped out too with the encode command).

    Cheers,
    Dave.

     
  • fb16
    fb16
    2012-05-21

    Hello davegb3,

    your help has been precious this time too. I know by now, that one issue will be solved following your hints :-)

    After simply added the escapes you suggested, now I can get the encoding for a selected chunk of text, only for the text entities (not those belonging to the html source code like &gt;). It is exactly what I was looking for.

    The final script:

    notepad.runMenuCommand("HTML Tag", "Encode HTML entities")
    notepad.runPluginCommand("XML Tools", "Convert selection text to XML (&&lt;&&gt; => <>)")
    

    And thank you very much for your suggestions too

    Regards

     
  • fb16
    fb16
    2012-10-28

    @davegb3

    I'm not expert of PY,  I'd be glad if you could help me with another of your very helpful hints. I noticed that after the recent upgrade to the recent version of HTMLTag, the following PY commands (which I was using since then as a PY script), don't work anymore:

    notepad.runMenuCommand("HTML Tag", "Encode HTML entities")
    notepad.runPluginCommand("XML Tools", "Convert selection text to XML (&&lt;&&gt; => <>)")

    :-) Thank you
    fb16