Jinja: wordwrap ν•„ν„°λŠ” κΈ°μ‘΄ 쀄 λ°”κΏˆμ„ λ¬΄μ‹œν•©λ‹ˆλ‹€.

에 λ§Œλ“  2013λ…„ 02μ›” 05일  Β·  9μ½”λ©˜νŠΈ  Β·  좜처: pallets/jinja

μ•ˆλ…•ν•˜μ„Έμš”,

이 ν•„ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ (예λ₯Ό λ“€μ–΄) 이메일 λ©”μ‹œμ§€λ₯Ό κ°€λŠ₯ν•œ ν•œ 자주 λ©‹μ§€κ²Œ ν‘œμ‹œν•˜κΈ° μœ„ν•΄ 항상 72자둜 μ€„λ°”κΏˆν•˜λ„λ‘ ν•  λ•Œ wordwrap이 κΈ°μ‘΄ 쀄 λ°”κΏˆμ„ μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€λŠ” 것을 μ•Œμ•˜μŠ΅λ‹ˆλ‹€. λ©”μ‹œμ§€λ₯Ό λ‹¨μ„œλ‘œ μ‚¬μš©ν•˜μ§€λ§Œ λŒ€μ‹  μ—„κ²©ν•˜κ²Œ 72μžλ§ˆλ‹€ κ°œν–‰ 문자λ₯Ό μ‚½μž…ν•©λ‹ˆλ‹€.

이것은 우리의 ν•΄κ²° λ°©λ²•μž…λ‹ˆλ‹€

## Workaround bug in do_wordwrap where it disregards existing linebreaks 
## when wrapping text

from jinja2.filters import environmentfilter
import re
<strong i="8">@environmentfilter</strong>
def do_wordwrap(environment, s, width=79, break_long_words=True):
    """
    Return a copy of the string passed to the filter wrapped after
    ``79`` characters.  You can override this default using the first
    parameter.  If you set the second parameter to `false` Jinja will not
    split words apart if they are longer than `width`.
    """
    import textwrap
    accumulator = []
    # Workaround: pre-split the string
    for component in re.split(r"\r?\n", s):
        # textwrap will eat empty strings for breakfirst. Therefore we route them around it.
        if len(component) is 0:
            accumulator.append(component)
            continue
        accumulator.extend(
            textwrap.wrap(component, width=width, expand_tabs=False,
                replace_whitespace=False,
                break_long_words=break_long_words)
        )
    return environment.newline_sequence.join(accumulator)

멋지고 μ™„λ²½ν•˜μ§€λŠ” μ•Šμ§€λ§Œ 우리λ₯Ό μœ„ν•΄ μž‘λ™ν•©λ‹ˆλ‹€.

wordrwap ν•„ν„°λ₯Ό 훨씬 더 μœ μš©ν•˜κ²Œ λ§Œλ“€κΈ° λ•Œλ¬Έμ— 이(λ˜λŠ” μœ μ‚¬ν•œ 것)κ°€ jinja2에 μ μ ˆν•˜κ²Œ ν¬ν•¨λ˜κΈ°λ₯Ό λ°”λžλ‹ˆλ‹€.

μ–΄λ–»κ²Œ μƒκ°ν•˜λ‚˜μš”?

λͺ¨λ“  9 λŒ“κΈ€

λ‚˜λŠ” 이것이 버그라고 μƒκ°ν•˜μ§€ μ•ŠλŠ”λ‹€. Wordwrap은 ν…μŠ€νŠΈκ°€ 아직 쀄 λ°”κΏˆλ˜μ§€ μ•Šμ•˜λ‹€κ³  κ°€μ •ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” 이것이 버그라고 μƒκ°ν•˜μ§€ μ•ŠλŠ”λ‹€. Wordwrap은 ν…μŠ€νŠΈκ°€ 아직 쀄 λ°”κΏˆλ˜μ§€ μ•Šμ•˜λ‹€κ³  κ°€μ •ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

κΈ€μŽ„, λΆ„λͺ…νžˆ. κ·ΈλŸ¬λ‚˜ 쀄 λ°”κΏˆμ΄ ν•„μš”ν•˜μ§€λ§Œ 쀄 λ°”κΏˆν•˜λ €λŠ” ν…μŠ€νŠΈμ— 이미 일뢀 ν˜•μ‹μ˜ μ„œμ‹μ΄ ν¬ν•¨λ˜μ–΄ μžˆλŠ” ν™˜κ²½μ—μ„œλŠ” μ‚¬μš©ν•˜κΈ°κ°€ 정말 μ–΄λ ΅μŠ΅λ‹ˆλ‹€(예: 단락 뢄리).

이것에 λŒ€ν•œ μ†Œμ‹μ΄ μžˆμŠ΅λ‹ˆκΉŒ? 단락이 끝에 쀄 λ°”κΏˆλ˜λ„λ‘ ν•΄μ•Ό ν•˜μ§€λ§Œ 단락 λ‚΄μ˜ 쀄이 μžλ™μœΌλ‘œ 쀄 λ°”κΏˆλ˜κΈ°λ₯Ό μ›ν•˜λŠ” 일반 ν…μŠ€νŠΈ 이메일이 μžˆμŠ΅λ‹ˆλ‹€. 이것은 일반적인 μ‚¬μš© 사둀인 것 κ°™μŠ΅λ‹ˆλ‹€. 사싀, λ‚΄κ°€ 이 λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜λŠ” 이 λŒ“κΈ€ μƒμžλ„ 같은 역할을 ν•©λ‹ˆλ‹€. 이 쀄은 μžλ™μœΌλ‘œ 쀄 λ°”κΏˆλ˜μ§€λ§Œ 두 번 μž…λ ₯ν•˜λ©΄

μƒˆλ‘œμš΄ 단락이 μ‹œμž‘λ©λ‹ˆλ‹€.

이것을 μœ„ν•΄ stdlib 이메일 λͺ¨λ“ˆμ„ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆκΉŒ?

μ–΄λŠ μ‹œμ μ— 이것을 μ‚½μž…ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?
μš°λ¦¬λŠ” Jinja2λ₯Ό μ‚¬μš©ν•˜μ—¬ 메일을 μƒμ„±ν•˜κ³  ν˜„μ§€ν™”ν•˜κ³  ν…œν”Œλ¦Ώμ„ μ»΄νŒŒμΌν•œ ν›„ κ²°κ³Ό λ¬Έμžμ—΄μ„ ν”ΌλΌλ―Έλ“œ_mailer/repoze_sendmail에 μ „λ‹¬ν•©λ‹ˆλ‹€.

일반 ν…μŠ€νŠΈ 이메일 ν…œν”Œλ¦Ώμ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

{%- filter wordwrap(width=72, break_long_words=False) -%}
{% block greeting -%}
{% trans full_name = _(user.full_name) %}Hello {{ full_name }},{% endtrans %}
{% endblock -%}

{% block message_intro %}
{% endblock -%}

{% trans -%}
This may be a very long text in another language, depending on what a translator put into the gettext localization. It may even have its own paragraphs.
{%- endtrans %}

...

λ‚˜λŠ” λ˜ν•œμ΄ λ¬Έμ œκ°€ λ°œμƒν–ˆκ³  Google 검색이 λ‚˜λ₯Ό μ—¬κΈ°λ‘œ μ΄λŒμ—ˆμŠ΅λ‹ˆλ‹€. @dwt 의 패치λ₯Ό μ‚¬μš©ν•˜λ©΄ νš¨κ³Όμ μž…λ‹ˆλ‹€. λ‚˜λŠ” λ˜ν•œ 이 λ³€κ²½ 사항이 μ½”λ“œ κΈ°λ°˜μ— ν†΅ν•©λ˜λŠ” 것에 νˆ¬ν‘œν•©λ‹ˆλ‹€.

μ—¬κΈ°μ—μ„œλ„ λ§ˆμ°¬κ°€μ§€λ‘œ μˆ˜μ • 사항을 λ§ˆμŠ€ν„°μ— ν†΅ν•©ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€!

wrapstring λŠ” νŒ¨μΉ˜μ—μ„œ μ‚¬λΌμ‘ŒμŠ΅λ‹ˆλ‹€.

μ—¬λŸ¬ 단락을 μ²˜λ¦¬ν•  λ•Œ textwrap.wrap() 의 λ‹€μ†Œ μ˜ˆμƒμΉ˜ λͺ»ν•œ λ™μž‘μ„ ν•΄κ²°ν•˜λŠ” ν’€ λ¦¬ν€˜μŠ€νŠΈλ₯Ό μ œμΆœν–ˆμŠ΅λ‹ˆλ‹€.

wrapstring 및 빈 쀄이 μœ μ§€λ©λ‹ˆλ‹€.

λ‹€λ₯Έ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ μΌλΆ€λ‘œ 이메일 본문을 μƒμ„±ν•˜κΈ° μœ„ν•΄ 이 κΈ°λŠ₯을 μ‚¬μš©ν•˜κ³  있으며 80μ—΄μ—μ„œ μž…λ ₯을 λž˜ν•‘ν•˜λ €κ³  ν•©λ‹ˆλ‹€.

λ‚΄ nbconvert ν…œν”Œλ¦Ώμ—μ„œλ„ 이 λ¬Έμ œκ°€ λ°œμƒν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. λ‚΄ μžμ‹ μ˜ ν•„ν„°λ₯Ό μž‘μ„±ν•΄μ•Ό ν•˜λŠ” 경우 nbconvert용 μ‚¬μš©μž 지정 내보내기λ₯Ό μž‘μ„±ν•΄μ•Ό ν•œλ‹€κ³  μƒκ°ν•˜κΈ° λ•Œλ¬Έμ— #766을 λ³‘ν•©ν•˜λŠ” 것이 맀우 쒋을 κ²ƒμž…λ‹ˆλ‹€. μ‚¬μš© νŽΈμ˜μ„±μ„ μœ„ν•΄ ν”Όν•˜λ €κ³  ν•©λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰