| 175 | } |
| 176 | |
| 177 | void SpecEmitter::writeExampleToDiskAndAppendToAppendix( |
| 178 | const std::string &exampleName, const std::string &exampleAnchor, |
| 179 | const std::string &fileName, const std::string &example) { |
| 180 | // If the example directory is not set, do nothing. |
| 181 | if (!this->examplesDirectory) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | llvm::SmallVector<char, 128> filePath; |
| 186 | // The path to write the example file to in the build directory. |
| 187 | llvm::sys::path::append(filePath, this->examplesDirectory.value(), fileName); |
| 188 | |
| 189 | // The relative path to the example file in the spec. |
| 190 | std::string relativePath = "/_spec_gen/examples/" + fileName; |
| 191 | |
| 192 | // Add an anchor to the example |
| 193 | this->appendixFile << ".. _" << exampleAnchor << ":\n\n"; |
| 194 | |
| 195 | // Add example name as header and example content |
| 196 | auto exampleNameWithIgnore = ":spelling:ignore:`" + exampleName + "`"; |
| 197 | this->appendixFile << "\n" << exampleNameWithIgnore << "\n"; |
| 198 | this->appendixFile << std::string(exampleNameWithIgnore.length(), '~') |
| 199 | << "\n\n"; |
| 200 | this->appendixFile << ".. literalinclude:: " << relativePath << "\n"; |
| 201 | this->appendixFile << indent << ":language: " |
| 202 | << "mlir" |
| 203 | << "\n\n"; |
| 204 | // Indent example content |
| 205 | // Create directories if they don't exist |
| 206 | std::error_code ec = |
| 207 | llvm::sys::fs::create_directories(this->examplesDirectory.value()); |
| 208 | |
| 209 | if (ec) { |
| 210 | llvm::errs() << "Error creating directory " |
| 211 | << this->examplesDirectory.value() << ": " << ec.message() |
| 212 | << "\n"; |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | // Open file for writing |
| 217 | std::error_code writeEC; |
| 218 | llvm::raw_fd_ostream outFile(std::string(filePath.begin(), filePath.end()), |
| 219 | writeEC); |
| 220 | if (writeEC) { |
| 221 | llvm::errs() << "Error opening file " << filePath << ": " |
| 222 | << writeEC.message() << "\n"; |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | // Write content to file |
| 227 | outFile << example; |
| 228 | outFile.close(); |
| 229 | } |
| 230 | |
| 231 | void SpecEmitter::emitExample(const std::string &exampleName, |
| 232 | const FormattedExample &formattedExample) { |