Coverage for sources/librovore/structures/sphinx/converters.py: 0%
20 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-28 22:09 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-28 22:09 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
4#============================================================================#
5# #
6# Licensed under the Apache License, Version 2.0 (the "License"); #
7# you may not use this file except in compliance with the License. #
8# You may obtain a copy of the License at #
9# #
10# http://www.apache.org/licenses/LICENSE-2.0 #
11# #
12# Unless required by applicable law or agreed to in writing, software #
13# distributed under the License is distributed on an "AS IS" BASIS, #
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15# See the License for the specific language governing permissions and #
16# limitations under the License. #
17# #
18#============================================================================#
21''' Sphinx-specific code block converters for universal pattern system. '''
24from . import __
25from .patterns import UNIVERSAL_PATTERNS as _UNIVERSAL_PATTERNS
28def convert_code_block_to_markdown( soup_element: __.typx.Any ) -> str:
29 ''' Converts code block to markdown fenced code block. '''
30 language = extract_code_language( soup_element )
31 code_content = soup_element.get_text( )
32 return f"```{language}\n{code_content}\n```"
35def extract_code_language( code_element: __.typx.Any ) -> str:
36 ''' Extracts programming language from code blocks. '''
37 parent = code_element.parent
38 if not parent: return ''
39 classes = parent.get( 'class', [ ] )
40 language_config = _UNIVERSAL_PATTERNS[ 'code_blocks' ][
41 'language_detection'
42 ]
43 prefix = language_config[ 'prefix' ]
44 language_map = language_config[ 'language_map' ]
45 for cls in classes:
46 if cls.startswith( prefix ):
47 language = cls.replace( prefix, '' )
48 return _map_language( language, language_map )
49 return ''
52def _map_language(
53 language: str,
54 language_map: __.cabc.Mapping[ str, str ]
55) -> str:
56 ''' Maps language names to standard markdown identifiers. '''
57 return language_map.get( language, language )