(base_url: str,
example_script_paths: list[Path],
doc_paths: list[Path],
extra_content="")
| 142 | |
| 143 | # Generate the example docs for each example script |
| 144 | def write_scripts(base_url: str, |
| 145 | example_script_paths: list[Path], |
| 146 | doc_paths: list[Path], |
| 147 | extra_content="") -> list[DocMeta]: |
| 148 | metas = [] |
| 149 | for script_path, doc_path in zip(example_script_paths, doc_paths): |
| 150 | if script_path.name in ignore_list: |
| 151 | logging.warning(f"Ignoring file: {script_path.name}") |
| 152 | continue |
| 153 | script_url = f"{base_url}/{script_path.name}" |
| 154 | # Determine language based on file extension |
| 155 | language = "python" if script_path.suffix == ".py" else "bash" |
| 156 | |
| 157 | # Make script_path relative to doc_path and call it include_path |
| 158 | include_path = '../../..' / script_path.relative_to(root_dir) |
| 159 | |
| 160 | # Extract metadata from the script file |
| 161 | if meta := extract_meta_info(str(script_path)): |
| 162 | title = underline(meta.title) |
| 163 | else: |
| 164 | logging.warning( |
| 165 | f"No metadata found for {script_path.name}, using filename as title" |
| 166 | ) |
| 167 | title = script_path.stem.replace('_', ' ').title() |
| 168 | meta = DocMeta(title=title, |
| 169 | order=0, |
| 170 | section="", |
| 171 | filename=script_path) |
| 172 | title = underline(title) |
| 173 | metas.append(meta) |
| 174 | |
| 175 | # Get line ranges excluding metadata |
| 176 | lines_without_metadata = _get_lines_without_metadata( |
| 177 | str(script_path)) |
| 178 | |
| 179 | # Build literalinclude directive |
| 180 | literalinclude_lines = [f".. literalinclude:: {include_path}"] |
| 181 | if lines_without_metadata: |
| 182 | literalinclude_lines.append( |
| 183 | f" :lines: {lines_without_metadata}") |
| 184 | literalinclude_lines.extend( |
| 185 | [f" :language: {language}", f" :linenos:"]) |
| 186 | |
| 187 | content = (f"{title}\n" |
| 188 | f"{extra_content}" |
| 189 | f"Source {script_url}.\n\n" |
| 190 | f"{chr(10).join(literalinclude_lines)}\n") |
| 191 | with open(doc_path, "w+") as f: |
| 192 | logging.warning(f"Writing {doc_path}") |
| 193 | f.write(content) |
| 194 | |
| 195 | return metas |
| 196 | |
| 197 | def write_index(metas: list[DocMeta], doc_template_path: Path, |
| 198 | doc_path: Path, example_name: str, |
no test coverage detected