You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add support for appending and prepending file content
This commit introduces two new file manipulation actions: "Append File" and "Prepend File". Users can now define these actions in their markdown input to add content to the end or beginning of specified files.
Key features and changes:
- **New Headers:** Recognizes `## Append File: <path>` / `**Append File: <path>**` and `## Prepend File: <path>` / `**Prepend File: <path>**`. These headers must be followed by a code block containing the content to append or prepend.
- **File Creation:** If the target file for an append or prepend action does not exist, it will be created with the specified content.
- **Parser Updates:**
- `ActionType` enum extended with `Append` and `Prepend`.
- Header parsing logic (`header_utils.rs`, `external_header.rs`, `internal_standard_handler.rs`, `wrapped_header.rs`) updated to recognize and process these new action types.
- `constants.rs` updated with new action keywords.
- **Processor Implementation:**
- New modules `src/processor/append.rs` and `src/processor/prepend.rs` implement the core logic.
- `action_handler.rs` dispatches to these new handlers.
- Error handling (`errors.rs`) includes new variants for append/prepend to a directory (`TargetIsDirectoryForAppend`, `TargetIsDirectoryForPrepend`).
- **Core Types & Summary:**
- `Summary` struct in `core_types.rs` now includes fields for `appended`, `prepended`, `failed_isdir_append`, and `failed_isdir_prepend`.
- New status enums `AppendStatus` and `PrependStatus` introduced.
- CLI output (`cli/output.rs`) and summary updating logic (`processor/summary_updater.rs`) reflect these additions.
- **Documentation:** `README.md` updated to document the new actions, their syntax, and examples.
- **Testing:** Comprehensive integration tests added for parsing, processing, and CLI usage of append/prepend actions, including interactions with other actions and error scenarios.
This enhancement provides more granular control over file content generation, allowing users to build up files incrementally.
The tool reads a markdown file, identifies actions (like creating, deleting, or moving files) based on specific header formats, and executes these actions relative to a specified output directory.
9
+
The tool reads a markdown file, identifies actions (like creating, appending, prepending, deleting, or moving files) based on specific header formats, and executes these actions relative to a specified output directory.
10
10
11
11
## Features
12
12
13
13
* Supports creating files with content defined in code blocks.
14
+
* Supports appending content to existing files (creates if not exists).
15
+
* Supports prepending content to existing files (creates if not exists).
* "Wrapped" header format for associating headers with subsequent code blocks or for standalone delete/move actions.
18
-
* Automatic creation of parent directories for created or moved files.
19
-
* Safety checks to prevent writing or moving outside the target base directory.
20
+
* Automatic creation of parent directories for created, appended, prepended or moved files.
21
+
* Safety checks to prevent writing or moving files outside the target base directory.
20
22
* Option to force overwriting existing files (for create and move actions).
21
23
* Detailed summary output of actions performed, skipped, or failed.
22
24
* Pre-commit hooks configured for code quality and consistency.
@@ -67,7 +69,7 @@ strux [OPTIONS] <MARKDOWN_FILE>
67
69
* **Default:** `./project-generated`. This path is relative to the **current working directory** where you run the command.
68
70
* The directory will be created if it doesn't exist.
69
71
* The command will fail if the specified path exists but is not a directory.
70
-
*`-f`, `--force`: Overwrite existing files when a `File` or `Moved File` action targets a path that already exists as a file. Without this flag, existing files will be skipped. This flag does not allow replacing a directory with a file.
72
+
*`-f`, `--force`: Overwrite existing files when a `File` or `Moved File` action targets a path that already exists as a file. Without this flag, existing files will be skipped. This flag does not allow replacing a directory with a file. It does not currently affect `Append File` or `Prepend File` actions beyond their standard behavior (they will operate on existing files or create new ones).
71
73
*`-h`, `--help`: Print help information.
72
74
*`-V`, `--version`: Print version information.
73
75
@@ -104,7 +106,43 @@ fn main() {
104
106
```
105
107
````
106
108
107
-
**2. `Deleted File` Actions:**
109
+
**2. `Append File` Actions:**
110
+
111
+
These headers must be immediately followed by a fenced code block. The content of the code block will be appended to the specified file. If the file does not exist, it will be created with the content.
112
+
113
+
***Markdown Headers:**
114
+
*`## Append File: path/to/your/file.txt`
115
+
*`**Append File: path/to/your/file.txt**`
116
+
117
+
**Example (Append Header):**
118
+
119
+
````markdown
120
+
## Append File: logs/app.log
121
+
122
+
```
123
+
[INFO] Application started.
124
+
```
125
+
````
126
+
127
+
**3. `Prepend File` Actions:**
128
+
129
+
These headers must be immediately followed by a fenced code block. The content of the code block will be prepended to the specified file. If the file does not exist, it will be created with the content.
130
+
131
+
***Markdown Headers:**
132
+
*`## Prepend File: path/to/your/file.txt`
133
+
*`**Prepend File: path/to/your/file.txt**`
134
+
135
+
**Example (Prepend Header):**
136
+
137
+
````markdown
138
+
## Prepend File: config.ini
139
+
140
+
```ini
141
+
; Generated by Strux
142
+
```
143
+
````
144
+
145
+
**4. `Deleted File` Actions:**
108
146
109
147
These headers define files to be deleted. They should *not* be followed by a code block unless using the special case below.
These headers define files to be moved. They should *not* be followed by a code block. The keyword " to " (case-sensitive, with spaces) separates the source and destination paths.
128
166
@@ -138,9 +176,10 @@ These headers define files to be moved. They should *not* be followed by a code
138
176
## Moved File: temp/report.docx to final/official_report.docx
These headers can appear on the *first line* inside a code block to define the file path for a `File` action.
181
+
These headers can appear on the *first line* inside a code block to define the file path for a `File`, `Append File`, or `Prepend File` action.
182
+
*Supported types: `File` (e.g., `// File: path/to/file.ext`). Support for`Append File` and `Prepend File`in this format may be added in the future.*
144
183
145
184
*`// File: path/to/file.ext`: The header line itself is **excluded** from the file content. Supports paths in backticks (`// File:\`path with spaces.txt\``).
146
185
@@ -161,10 +200,10 @@ These headers can appear on the *first line* inside a code block to define the f
161
200
162
201
*Heuristics apply to avoid misinterpreting comments as paths.*
163
202
164
-
**5. Wrapped Headers:**
203
+
**7. Wrapped Headers:**
165
204
166
205
A header can be placed inside a ````markdown ` or ````md ` block.
167
-
* For `File` actions, it applies to the *next adjacent* code block.
206
+
* For `File`, `Append File`, or `Prepend File` actions, it applies to the *next adjacent* code block.
168
207
* For `Deleted File` or `Moved File` actions, it's a standalone action.
169
208
170
209
* **Create Example:**
@@ -180,6 +219,17 @@ A header can be placed inside a ` ```markdown ` or ` ```md ` block.
180
219
feature_a: true
181
220
```
182
221
````
222
+
* **Append Example:**
223
+
224
+
````markdown
225
+
```markdown
226
+
## Append File: notes.txt
227
+
```
228
+
229
+
```
230
+
Another note.
231
+
```
232
+
````
183
233
184
234
* **Delete Example:**
185
235
@@ -201,14 +251,14 @@ A header can be placed inside a ` ```markdown ` or ` ```md ` block.
201
251
### Path Handling and Safety
202
252
203
253
* Paths specified in headers are treated as relative to the `--output-dir`.
204
-
* Parent directories are created automatically as needed for `File` actions and for the destination of `Moved File` actions.
254
+
* Parent directories are created automatically as needed for `File`, `Append File`, `Prepend File` actions and for the destination of `Moved File` actions.
205
255
* **Safety:** The tool prevents writing or moving files outside the resolved base output directory. Paths containing `..` that would escape the base directory will cause the action to fail safely.
206
256
* Paths containing invalid components (like `//` or trailing `/`) will be skipped.
* The *entire* content within the fenced code block (excluding the fences themselves and certain internal headers) is written to the file.
211
-
* A trailing newline (`\n`) is added to the file content if it doesn't already end with one.
260
+
* The *entire* content within the fenced code block (excluding the fences themselves and certain internal headers) is written to the file (or appended/prepended).
261
+
* A trailing newline (`\n`) is added to this content chunk if it doesn't already end with one.
212
262
213
263
## Examples
214
264
@@ -246,6 +296,11 @@ Generated by Strux.
246
296
247
297
## Moved File: temp/draft.txt to docs/final_draft.txt
0 commit comments