Reference#
CODE_EXTENSION_TO_LANGUAGE = {'yml': 'yaml', 'j2': 'jinja'}
module-attribute
#
Map of file extensions to code language.
Used when pygments is not available.
includex(filepath, start=1, end=None, lines=0, dedent=True, indent=0, indent_char=' ', indent_first=False, keep_trailing_whitespace=False, start_match='', end_match='', start_offset=0, end_offset=0, include_end_match=False, silence_errors=False, raise_errors=True, raw=False, escape=None, replace=None, add_heading_levels=0, lang=None, escape_notice=True, replace_notice=False, caption=None, alt_code_fences=False, suffix='', code=False)
#
Include parts of a file.
Note
Whitespace (spaces, empty lines) will be stripped from the end of file. This prevents all includes of full files to end in a newline.
Parameters:
-
filepath
(Path
) –file to include
-
start
(int
, default:1
) –line number to begin include (is overwritten, if start_match matches a line).
-
end
(int
, default:None
) –line number to end include on (is overwritten, if end_match matches a line).
Use negative numbers to index from end of file (e.g. -3 to skip last 3 lines of file). Must be greater than start, otherwise no content will be returned.
-
lines
(int
, default:0
) –number of lines to return (takes precedence over end and end_match)
-
dedent
(bool
, default:True
) –dedent by indentation of first line to be returned
-
dedent
(int
, default:True
) –dedent by that many characters
-
indent
(int
, default:0
) –add this many indent_chars to beginning of each line
-
indent_char
(str
, default:' '
) –single character to use for indentation (default: " " → space)
-
indent_first
(bool
, default:False
) –whether to also indent the first line
indent
might be used to make the content match the indentation of the document where the content is included. As theincludex
statement will already be indented, the first line doesn't need to be indented in most cases where an indent would be used. -
start_match
(str
, default:''
) –find start by providing text that shall match the first line
-
end_match
(str
, default:''
) –find end by providing text that shall match the last line.
Warning
Cannot be used together with lines
-
start_offset
(int
, default:0
) –number of lines to offset line found by start_match provide positive integer to exclude that many lines after matched line provide negative integer to include additional lines before matched line
-
end_offset
(int
, default:0
) –number of lines to offset line found by end_match
- provide positive integer to include additional lines after matched line
- provide negative integer to exclude lines before matched line
-
include_end_match
(bool
, default:False
) –also include the ending matched line (same as
end_offset=1
) -
silence_errors
(bool
, default:False
) –if true, do not return exception messages
-
raise_errors
(bool
, default:True
) –if true, raise exceptions instead of returning error string
-
raw
(bool
, default:False
) –will wrap file content in {% raw %} block.
this prevents further interpretation of the file content by jinja.
-
escape
(list[str]
, default:None
) –characters in list will be escaped using
\
-
replace
(list[tuple[str]]
, default:None
) –replace arbitrary substrings
-
add_heading_levels
(int
, default:0
) –If > 0, append as many "#" to any line starting with "#"
this is meant to be used with Markdown files, that need to fit into an existing header structure
-
lang
(str
, default:None
) –wrap included file in markdown code fences with this language
this was added to support escaping an included file (using
raw
) but not have the raw-tags be part of the code block.Deprecated since v0.0.4, use
code
instead. -
escape_notice
(bool | str
, default:True
) –include note about escaped characters at the end
-
replace_notice
(bool | str
, default:False
) –include note about replaced strings at the end
-
caption
(bool | str
, default:None
) –include caption for code block
lang
must be given for this option to have any effect -
alt_code_fences
(bool | str
, default:False
) –when
True
,'''
is used for code fences so they are not rendered in Markdown documents.When a custom string is provided, it will be used as code fence marker instead.
-
suffix
(str
, default:''
) –add this after the included content
-
code
(bool | str
, default:False
) –render included content as code block
If
True
, the language will be inferred from the file extension. If""
, code block will be rendered without language. Otherwise, the given string will be used as code languageAdded in v0.0.4
Returns:
-
str
–content of file at filepath, modified by remaining arguments
Source code in includex.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
caption#
Include a caption with included content, that is rendered as a code block via lang.
The caption will have a .caption
css class applied via attr_list
. So ensure you have this markdown extension enabled in your mkdocs.yaml
:
Then you can style these captions to look just like figcaptions. Here is an example for Material for MkDocs:
/* center captions */
.md-typeset .caption,
.md-typeset figcaption {
/* same as material's figcaption */
font-style: normal;
max-width: 24rem;
margin: 1em auto 1.5em;
text-align: center;
display: block;
font-size: 0.8rem;
/* custom */
line-height: 1.2;
}
lang#
Wrap included content in code-fences with the given language.