PyCharm:Python 字符串中的 HTML 语法检查和突出显示 [英] PyCharm: HTML Syntax checking and highlighting in Python strings

查看:122
本文介绍了PyCharm:Python 字符串中的 HTML 语法检查和突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

@register.simple_tagdef last_orders(用户):行 = []对于 Order.objects.filter(user=user).order_by('-offer__date') 中的订单:rows.append(format_html('<tr><td>{}</td><td>{}</td><td>{}Eur</td><td class=";int col-1">{}</td><td>{} Eur</td></div>',order.offer.date, order.offer.text, order.offer.price, order.amount, order.amount*order.offer.price))如果不是行:return format_html('<div>目前没有订单.</div>')return format_html('''最后订单:<table class="table table-striped"><tr><th>日期</th><th>Text</th><th>Price</th><th class=col-1">Amount</th>;th>总和</th></tr>{}''', join(rows))

PyCharm 是否能够像上面那样在 Python 字符串中进行 HTML 语法检查和高亮显示?

解决方案

是的!PyCharm 两者都可以...

要在包含 HTML 的 Python 字符串中同时具备这两种功能,首先需要使用一种称为语言注入"的 IDE 功能.

在 IDE 的上下文中,语法检查" 通常称为 检查"语法突出显示" 位于配色方案",如下(与同节的Python条目对比):

<头>
功能IDE 对话路径
语法检查设置 > Editor > Inspections > HTML
语法亮点设置 > Editor > Color Sheme > HTML

使用语言注入:

我在 JetBrains 网站上找到的最佳指南是:

对 HTML 的语法检查(或检查):

此时,如果您按 Alt + 6(运行 Inspect Code 会给出额外警告),应该会出现一个带有 HTML 警告和语法检查的停靠窗口.您还将获得 HTML 代码提示:

语法要点:

最后的想法:

PyCharm 针对 HTML 发出的警告显示在下面的屏幕截图中.其他功能

Example:

@register.simple_tag
def last_orders(user):
    rows = []
    for order in Order.objects.filter(user=user).order_by('-offer__date'):
        rows.append(format_html('<tr><td>{}</td><td>{}</td><td>{} Eur</td><td class="int col-1">{}</td><td>{} Eur</td></div>',
                                order.offer.date, order.offer.text, order.offer.price, order.amount, order.amount*order.offer.price))
    if not rows:
        return format_html('<div>No orders up to now.</div>')
    return format_html('''Last Orders: 
     <table class="table table-striped">
      <tr><th>Date</th><th>Text</th><th>Price</th><th class="col-1">Amount</th><th>Sum</th></tr>
      {}
     </table>
     ''', join(rows))

Is PyCharm able to do HTML Syntax checking and highlighting in Python strings like above?

解决方案

Yes! PyCharm can do both...

To have both functionalities in a Python string containing HTML first requires using an IDE functionality called "Language injections".

In the context of the IDE "Syntax checks" are usually called "Inspections" and "Syntax highlighting" is under "Color Scheme", as follows (compare with the Python entries in the same sections):

Functionalities IDE path to dialogue
Syntax checks Settings > Editor > Inspections > HTML
Syntax highlights Settings > Editor > Color Sheme > HTML

Using Language Injections:

The best guide I've found on the JetBrains site is: "Language injections (for IntelliJ IDEA)", it follows the exact same steps as the guide for the PyCharm IDE but is more elaborate. (I also liked this youtube video that shows how to do "Language injections" for several different languages.)

I used the following Python code separating the string with a backslash \ because it's easier for seeing syntax error warnings on separate lines:

# language=HTML prefix=<body> suffix=<body>
your_python_string = '\
    <body text=""><h1>some header text</h1></body>\
    t<br></br> \
    <dir>\
    '

Having said that, lets configure the functionalities:

Start by clicking inside the Python string literal and press Alt + Enter or click in light bulb pop-up; choose "Inject language or reference" and next choose "HTML" as show below, the background color of the string literal should change:

Syntax checks (or Inspections) to the HTML:

At this point if you press Alt + 6 (running Inspect Code gives additional warnings) there should be a docked window with the HTML warnings and syntax check. You will also have HTML code hints available:

Syntax highlights:

This dialogue is integrated into the PyCharm per language, after having activated "Language injection" for the string literal it already has HTML specific highlight, you can adjust the color scheme for better contrast to your preference:

A final thought:

The warnings that are emitted by PyCharm for HTML are shown in the screenshot below. Other functionalities associated with HTML files are also available in the HTML injected string.

这篇关于PyCharm:Python 字符串中的 HTML 语法检查和突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆