Skip to content

Commit 867bfdb

Browse files
committed
implement option to disable codeblock wrapping
1 parent eea7bf2 commit 867bfdb

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ Add line numbers to source code blocks:
154154
code2prompt path/to/codebase --line-number
155155
```
156156

157+
Disable wrapping code inside markdown code blocks:
158+
159+
```sh
160+
code2prompt path/to/codebase --no-codeblock
161+
```
162+
157163
- Rewrite the code to another language.
158164
- Find bugs/security vulnerabilities.
159165
- Document the code.

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ struct Cli {
6565
#[clap(short, long)]
6666
line_number: bool,
6767

68+
/// Disable wrapping code inside markdown code blocks
69+
#[clap(long)]
70+
no_codeblock: bool,
71+
6872
/// Use relative paths instead of absolute paths, including the parent directory
6973
#[clap(long)]
7074
relative_paths: bool,
@@ -106,6 +110,7 @@ fn main() -> Result<()> {
106110
args.line_number,
107111
args.relative_paths,
108112
args.exclude_from_tree,
113+
args.no_codeblock,
109114
);
110115

111116
let (tree, files) = match create_tree {

src/path.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub fn traverse_directory(
3131
line_number: bool,
3232
relative_paths: bool,
3333
exclude_from_tree: bool,
34+
no_codeblock: bool,
3435
) -> Result<(String, Vec<serde_json::Value>)> {
3536
// ~~~ Initialization ~~~
3637
let mut files = Vec::new();
@@ -72,7 +73,7 @@ pub fn traverse_directory(
7273
if let Ok(code_bytes) = fs::read(path) {
7374
let code = String::from_utf8_lossy(&code_bytes);
7475

75-
let code_block = wrap_code_block(&code, path.extension().and_then(|ext| ext.to_str()).unwrap_or(""), line_number);
76+
let code_block = wrap_code_block(&code, path.extension().and_then(|ext| ext.to_str()).unwrap_or(""), line_number, no_codeblock);
7677

7778
if !code.trim().is_empty() && !code.contains(char::REPLACEMENT_CHARACTER) {
7879
let file_path = if relative_paths {
@@ -117,7 +118,8 @@ pub fn label<P: AsRef<Path>>(p: P) -> String {
117118
let path = p.as_ref();
118119
if path.file_name().is_none() {
119120
let current_dir = std::env::current_dir().unwrap();
120-
current_dir.file_name()
121+
current_dir
122+
.file_name()
121123
.and_then(|name| name.to_str())
122124
.unwrap_or(".")
123125
.to_owned()
@@ -136,11 +138,12 @@ pub fn label<P: AsRef<Path>>(p: P) -> String {
136138
/// * `code` - The code block to wrap.
137139
/// * `extension` - The file extension of the code block.
138140
/// * `line_numbers` - Whether to add line numbers to the code.
141+
/// * `no_codeblock` - Whether to not wrap the code block with a delimiter.
139142
///
140143
/// # Returns
141144
///
142145
/// * `String` - The wrapped code block.
143-
fn wrap_code_block(code: &str, extension: &str, line_numbers: bool) -> String {
146+
fn wrap_code_block(code: &str, extension: &str, line_numbers: bool, no_codeblock: bool) -> String {
144147
let delimiter = "`".repeat(3);
145148
let mut code_with_line_numbers = String::new();
146149

@@ -152,8 +155,12 @@ fn wrap_code_block(code: &str, extension: &str, line_numbers: bool) -> String {
152155
code_with_line_numbers = code.to_string();
153156
}
154157

155-
format!(
156-
"{}{}\n{}\n{}",
157-
delimiter, extension, code_with_line_numbers, delimiter
158-
)
158+
if no_codeblock {
159+
code_with_line_numbers
160+
} else {
161+
format!(
162+
"{}{}\n{}\n{}",
163+
delimiter, extension, code_with_line_numbers, delimiter
164+
)
165+
}
159166
}

0 commit comments

Comments
 (0)