Skip to content

Commit 46913ab

Browse files
committed
Introduce a comprehensive context sanitization system with
various rule types for content manipulation. This includes: - ContextSanitizer with modular rule interface - AstDocTransformer for code-to-documentation transformation - Various sanitization rules (CommentInsertion, KeywordRemoval, RegexReplacement)
1 parent c1e2e2c commit 46913ab

13 files changed

+1357
-8
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: 📚 Generate Documentation
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
paths:
7+
- 'context.json'
8+
workflow_dispatch: # Allow manual trigger
9+
release:
10+
types: [ published ]
11+
12+
jobs:
13+
generate-docs:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: write # Needed to push updates back to the repository
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
23+
- name: Setup PHP
24+
uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: '8.2'
27+
coverage: none
28+
29+
- name: Download context-generator
30+
run: |
31+
wget https://github.com/butschster/context-generator/releases/download/1.5.0/context-generator.phar
32+
chmod +x context-generator.phar
33+
./context-generator.phar version
34+
35+
- name: Generate documentation
36+
run: |
37+
./context-generator.phar
38+
39+
- name: List generated documents
40+
run: |
41+
find .context -type f | sort
42+
echo "Generated documentation files in .context directory"
43+
44+
- name: 📤 Upload documentation as release assets
45+
uses: softprops/[email protected]
46+
if: github.event_name == 'release'
47+
with:
48+
token: "${{ secrets.GITHUB_TOKEN }}"
49+
files: |
50+
.context/**/*

json-schema.json

Lines changed: 237 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,214 @@
538538
}
539539
}
540540
},
541+
"php-docs": {
542+
"type": "object",
543+
"properties": {
544+
"include_private_methods": {
545+
"type": "boolean",
546+
"description": "Whether to include private methods in documentation",
547+
"default": false
548+
},
549+
"include_protected_methods": {
550+
"type": "boolean",
551+
"description": "Whether to include protected methods in documentation",
552+
"default": true
553+
},
554+
"include_private_properties": {
555+
"type": "boolean",
556+
"description": "Whether to include private properties in documentation",
557+
"default": false
558+
},
559+
"include_protected_properties": {
560+
"type": "boolean",
561+
"description": "Whether to include protected properties in documentation",
562+
"default": true
563+
},
564+
"include_implementations": {
565+
"type": "boolean",
566+
"description": "Whether to include method implementations in code blocks",
567+
"default": true
568+
},
569+
"include_property_defaults": {
570+
"type": "boolean",
571+
"description": "Whether to include property default values",
572+
"default": true
573+
},
574+
"include_constants": {
575+
"type": "boolean",
576+
"description": "Whether to include class constants",
577+
"default": true
578+
},
579+
"code_block_format": {
580+
"type": "string",
581+
"description": "Language identifier for code blocks (e.g., 'php')",
582+
"default": "php"
583+
},
584+
"class_heading_level": {
585+
"type": "integer",
586+
"description": "Heading level for class names (1-6)",
587+
"minimum": 1,
588+
"maximum": 6,
589+
"default": 1
590+
},
591+
"extract_routes": {
592+
"type": "boolean",
593+
"description": "Whether to extract route information from annotations/attributes",
594+
"default": true
595+
},
596+
"keep_doc_comments": {
597+
"type": "boolean",
598+
"description": "Whether to keep doc comments in the output",
599+
"default": true
600+
}
601+
}
602+
},
603+
"sanitizer": {
604+
"type": "object",
605+
"properties": {
606+
"rules": {
607+
"type": "array",
608+
"description": "Array of sanitization rules to apply",
609+
"items": {
610+
"type": "object",
611+
"oneOf": [
612+
{
613+
"type": "object",
614+
"properties": {
615+
"type": {
616+
"type": "string",
617+
"enum": [
618+
"keyword"
619+
],
620+
"description": "Keyword removal rule"
621+
},
622+
"name": {
623+
"type": "string",
624+
"description": "Optional unique rule name"
625+
},
626+
"keywords": {
627+
"type": "array",
628+
"items": {
629+
"type": "string"
630+
},
631+
"description": "Array of keywords to search for and remove"
632+
},
633+
"replacement": {
634+
"type": "string",
635+
"description": "Text to replace the found keywords with"
636+
},
637+
"caseSensitive": {
638+
"type": "boolean",
639+
"description": "Whether matching should be case-sensitive"
640+
},
641+
"removeLines": {
642+
"type": "boolean",
643+
"description": "Whether to remove entire lines containing the keyword"
644+
}
645+
},
646+
"required": [
647+
"type",
648+
"keywords"
649+
]
650+
},
651+
{
652+
"type": "object",
653+
"properties": {
654+
"type": {
655+
"type": "string",
656+
"enum": [
657+
"regex"
658+
],
659+
"description": "Regular expression replacement rule"
660+
},
661+
"name": {
662+
"type": "string",
663+
"description": "Optional unique rule name"
664+
},
665+
"patterns": {
666+
"type": "object",
667+
"additionalProperties": {
668+
"type": "string"
669+
},
670+
"description": "Object mapping regex patterns to their replacements"
671+
},
672+
"usePatterns": {
673+
"type": "array",
674+
"items": {
675+
"type": "string",
676+
"enum": [
677+
"credit-card",
678+
"email",
679+
"api-key",
680+
"ip-address",
681+
"jwt",
682+
"phone-number",
683+
"password-field",
684+
"url",
685+
"social-security",
686+
"aws-key",
687+
"private-key",
688+
"database-conn"
689+
]
690+
},
691+
"description": "Predefined pattern aliases to use"
692+
}
693+
},
694+
"required": [
695+
"type"
696+
]
697+
},
698+
{
699+
"type": "object",
700+
"properties": {
701+
"type": {
702+
"type": "string",
703+
"enum": [
704+
"comment"
705+
],
706+
"description": "Comment insertion rule"
707+
},
708+
"name": {
709+
"type": "string",
710+
"description": "Optional unique rule name"
711+
},
712+
"fileHeaderComment": {
713+
"type": "string",
714+
"description": "Comment to insert at the top of file"
715+
},
716+
"classComment": {
717+
"type": "string",
718+
"description": "Comment to insert before class definitions"
719+
},
720+
"methodComment": {
721+
"type": "string",
722+
"description": "Comment to insert before method definitions"
723+
},
724+
"frequency": {
725+
"type": "integer",
726+
"minimum": 0,
727+
"description": "How often to insert random comments (0 = disabled, 1 = every line, 5 = every 5th line)"
728+
},
729+
"randomComments": {
730+
"type": "array",
731+
"items": {
732+
"type": "string"
733+
},
734+
"description": "Array of random comments to insert"
735+
}
736+
},
737+
"required": [
738+
"type"
739+
]
740+
}
741+
],
742+
"required": [
743+
"type"
744+
]
745+
}
746+
}
747+
}
748+
},
541749
"modifiers": {
542750
"type": "array",
543751
"description": "List of content modifiers to apply",
@@ -551,7 +759,8 @@
551759
"type": "string",
552760
"description": "Modifier identifier",
553761
"enum": [
554-
"php-content-filter"
762+
"php-signature",
763+
"php-docs"
555764
]
556765
}
557766
},
@@ -569,7 +778,9 @@
569778
"type": "string",
570779
"description": "Modifier identifier",
571780
"enum": [
572-
"php-content-filter"
781+
"php-content-filter",
782+
"php-docs",
783+
"sanitizer"
573784
]
574785
},
575786
"options": {
@@ -586,6 +797,30 @@
586797
"then": {
587798
"$ref": "#/definitions/php-content-filter"
588799
}
800+
},
801+
{
802+
"if": {
803+
"properties": {
804+
"name": {
805+
"const": "php-docs"
806+
}
807+
}
808+
},
809+
"then": {
810+
"$ref": "#/definitions/php-docs"
811+
}
812+
},
813+
{
814+
"if": {
815+
"properties": {
816+
"name": {
817+
"const": "sanitizer"
818+
}
819+
}
820+
},
821+
"then": {
822+
"$ref": "#/definitions/sanitizer"
823+
}
589824
}
590825
]
591826
}

src/Cli/ContextGenerator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Butschster\ContextGenerator\Loader\CompositeDocumentsLoader;
1818
use Butschster\ContextGenerator\Loader\ConfigDocumentsLoader;
1919
use Butschster\ContextGenerator\Loader\JsonConfigDocumentsLoader;
20+
use Butschster\ContextGenerator\Modifier\AstDocTransformer;
21+
use Butschster\ContextGenerator\Modifier\ContextSanitizerModifier;
2022
use Butschster\ContextGenerator\Modifier\PhpContentFilter;
2123
use Butschster\ContextGenerator\Modifier\PhpSignature;
2224
use Butschster\ContextGenerator\Modifier\SourceModifierRegistry;
@@ -48,8 +50,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4850
{
4951
$files = new Files();
5052
$modifiers = new SourceModifierRegistry();
51-
$modifiers->register(new PhpSignature());
52-
$modifiers->register(new PhpContentFilter());
53+
$modifiers->register(
54+
new PhpSignature(),
55+
new ContextSanitizerModifier(),
56+
new PhpContentFilter(),
57+
new AstDocTransformer(),
58+
);
5359

5460
// Create GitHub-related components
5561
$githubToken = \getenv('GITHUB_TOKEN') ?: null;

src/Fetcher/FileSourceFetcher.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ public function fetch(SourceInterface $source): string
5858
$fileName = $file->getFilename();
5959
$filePath = empty($relativePath) ? $fileName : "$relativePath/$fileName";
6060

61-
$content .= "```\n";
6261
$content .= "// Path: {$filePath}\n";
6362
$content .= $this->getContent($file, $source) . "\n\n";
64-
$content .= "```\n";
6563
}
6664

6765
return $content;

0 commit comments

Comments
 (0)