> For AI agents: the complete documentation index is available at https://canary.gukhanmun.org/ko-KR/llms.txt, the full documentation bundle is available at https://canary.gukhanmun.org/ko-KR/llms-full.txt.

# HTML 처리

HTML 조각(fragment)을 변환하려면 `InputFormat.HTML`을 사용합니다. 태그와
속성은 출력에 남고, 변환은 대상 텍스트 노드(node)에 적용됩니다.

```python
from gukhanmun import Converter, DictionaryEntry, InputFormat, MapDictionarySource

converter = Converter(dictionaries=[MapDictionarySource([
    DictionaryEntry("漢字", "한자"),
])])
assert converter.convert("<p>漢字</p>", format=InputFormat.HTML) == "<p>한자</p>"
```

## 기본 보존

`<pre>`, `<code>`, `<kbd>`, `<script>`, `<style>`, `<textarea>`는 그 내용과
자손을 보존합니다. 계승한 `lang` 속성도 변환을 제어합니다. 한국어 태그인
`ko`나 `kor`, 또는 `ko-KR`처럼 하위 태그를 더한 형태는 대소문자 구별 없이
변환을 허용하며, 다른 언어
태그는 텍스트를 보존합니다. 내부의 `lang`은 계승한 언어를 바꿀 수 있습니다.

```python
assert converter.convert("<code>漢字</code>", format=InputFormat.HTML) == (
    "<code>漢字</code>"
)
```

`translate="no"` 같은 다른 보존 관례에는 명시적인 규칙을 사용합니다.

## 사용자 정의 보존

`HtmlOptions`는 클래스(class)와 속성 규칙을 더합니다. 어느 규칙이든 요소에
일치하면 그 요소와 모든 자손을 보존합니다.

```python
from gukhanmun import AttributeMatcher, HtmlOptions

converter = Converter(
    dictionaries=[MapDictionarySource([DictionaryEntry("漢字", "한자")])],
    html=HtmlOptions(
        preserve_classes=["keep"],
        preserve_attributes=[
            AttributeMatcher("data-keep"),
            AttributeMatcher("translate", "no"),
        ],
    ),
)
assert converter.convert(
    '<p class="keep"><b>漢字</b></p>', format=InputFormat.HTML,
) == '<p class="keep"><b>漢字</b></p>'
```

클래스명은 ASCII 공백으로 나뉜 토큰(token)이며 대소문자를 구별합니다.
`AttributeMatcher(name)`은 존재를, `AttributeMatcher(name, value)`는 디코딩한
값을 검사합니다. 속성명은 ASCII 대소문자를 구별하지 않지만 값은 구별합니다.
`AttributeMatcher` 객체를 전달해야 하며, 속성명 문자열만은 받지 않습니다.

## 루비 마크업

HTML에 `RenderMode.RUBY_ON_HANGUL`, `RUBY_ON_HANJA`, 또는
`OriginalGloss.RUBY`를 지정한 원문 모드를 결합하면 루비(ruby) 주해(annotation)를
만듭니다. `<title>`·`<option>` 같은 텍스트 전용 요소는 괄호 폴백(fallback)을
사용합니다. 완전한 예제는 〈[렌더링 모드](https://canary.gukhanmun.org/ko-KR/guide/python/rendering.md#%EB%A3%A8%EB%B9%84-%EB%A7%88%ED%81%AC%EC%97%85)〉를
참조하십시오.

## 오류 복구

기본값인 `Recovery.STRICT`는 HTML 스캐너(scanner)가 처리할 수 없는 잘못된
입력을 만나면 `GukhanmunError`를 발생시킵니다. `Recovery.LENIENT`는 잘못된
범위를 그대로 보존하고 인식할 수 있는 부분은 변환합니다.

```python
from gukhanmun import Recovery

converter = Converter(recovery=Recovery.LENIENT)
assert converter.convert(
    "<p>學校 <![CDATA[北京</p>", format=InputFormat.HTML,
) == "<p>학교 <![CDATA[北京</p>"
```

관대한 복구는 HTML을 수복하거나 무해화하지 않습니다. 인식하지 못한 범위를
유지하며, 끝나지 않은 구조의 나머지 전체가 그 범위에 들어갈 수도 있습니다.
이 옵션은 평문 텍스트나 Markdown에 영향을 주지 않습니다. 코드와 원인 연쇄는
[오류 참고 문서](https://canary.gukhanmun.org/ko-KR/api/python.md#%EC%98%A4%EB%A5%98)를 참조하십시오.
