文件树转化
前言
在用到vuepress的plume主题的时候,发现一个可以展示文件树的功能,但是windows的tree命令产生的结果并不能直接使用在file-tree中,所以需要进行转化。
然后让GPT写了一个Python脚本进行转化。
脚本
import re
def txt_to_md(input_file, output_file):
# 打开输入文件和输出文件
with open(input_file, 'r', encoding='utf-16') as infile, open(output_file, 'w', encoding='utf-8') as outfile:
indent_stack = [0] # 用于追踪缩进层级
previous_indent_level = -1 # 上一行的缩进层级
for line in infile:
# 去掉前后的空白符
stripped_line = line.strip()
if not stripped_line:
continue
# 计算当前行的缩进层级
indent_level = len(line) - len(line.lstrip('│├└ '))
# 获取文件或文件夹名称
name = re.sub(r'^[│├└─ ]+', '', stripped_line)
if not name:
continue
# 如果缩进层级减少,则需要关闭多余的层级
while indent_stack and indent_stack[-1] >= indent_level:
indent_stack.pop()
# 写入文件/文件夹结构
outfile.write(f"{' ' * len(indent_stack)}- {name}\n")
indent_stack.append(indent_level) # 将当前层级压入堆栈
# 更新上一行缩进层级
previous_indent_level = indent_level
# 调用函数
txt_to_md("WindowsProjectTree.txt", "PlumeFileTree.md")
说明
其中WindowsProjectTree.txt是windows的tree命令产生的结果,PlumeFileTree.md是转化后的结果。
输入输出示例
WindowsProjectTree.txt
│ .gitignore
│ build.gradle.kts
│ gradle.properties
│ gradlew
│ gradlew.bat
│ local.properties
│ settings.gradle.kts
│
├─.gradle
│ │ config.properties
│ │ file-system.probe
│ │
│ ├─8.7
│ │ │ gc.properties
│ │ │
│ │ ├─checksums
│ │ │ checksums.lock
│ │ │ md5-checksums.bin
│ │ │ sha1-checksums.bin
│ │ │
│ │ ├─dependencies-accessors
│ │ │ │ gc.properties
│ │ │ │
│ │ │ └─e5d0c485ed9081f602aa0c935b664eb63f940590
│ │ │ │ metadata.bin
│ │ │ │
│ │ │ ├─classes
│ │ │ │ └─org
│ │ │ │ └─gradle
│ │ │ │ └─accessors
│ │ │ │ └─dm
│ │ │ │ LibrariesForLibs$AndroidPluginAccessors.class
│ │ │ │ LibrariesForLibs$AndroidxCoreLibraryAccessors.class
│ │ │ │ LibrariesForLibs$AndroidxEspressoLibraryAccessors.class
│ │ │ │ LibrariesForLibs$AndroidxLibraryAccessors.class
│ │ │ │ LibrariesForLibs$BundleAccessors.class
│ │ │ │ LibrariesForLibs$JetbrainsKotlinPluginAccessors.class
│ │ │ │ LibrariesForLibs$JetbrainsPluginAccessors.class
│ │ │ │ LibrariesForLibs$PluginAccessors.class
│ │ │ │ LibrariesForLibs$VersionAccessors.class
│ │ │ │ LibrariesForLibs.class
│ │ │ │ LibrariesForLibsInPluginsBlock$AndroidPluginAccessors.class
│ │ │ │ LibrariesForLibsInPluginsBlock$AndroidxCoreLibraryAccessors.class
│ │ │ │ LibrariesForLibsInPluginsBlock$AndroidxEspressoLibraryAccessors.class
│ │ │ │ LibrariesForLibsInPluginsBlock$AndroidxLibraryAccessors.class
│ │ │ │ LibrariesForLibsInPluginsBlock$BundleAccessors.class
│ │ │ │ LibrariesForLibsInPluginsBlock$JetbrainsKotlinPluginAccessors.class
│ │ │ │ LibrariesForLibsInPluginsBlock$JetbrainsPluginAccessors.class
│ │ │ │ LibrariesForLibsInPluginsBlock$PluginAccessors.class
│ │ │ │ LibrariesForLibsInPluginsBlock$VersionAccessors.class
│ │ │ │ LibrariesForLibsInPluginsBlock.class
│ │ │ │
│ │ │ └─sources
│ │ │ └─org
│ │ │ └─gradle
│ │ │ └─accessors
│ │ │ └─dm
│ │ │ LibrariesForLibs.java
│ │ │ LibrariesForLibsInPluginsBlock.java
│ │ │
│ │ ├─executionHistory
│ │ │ executionHistory.lock
│ │ │
│ │ ├─expanded
│ │ ├─fileChanges
│ │ │ last-build.bin
│ │ │
│ │ ├─fileHashes
│ │ │ fileHashes.bin
│ │ │ fileHashes.lock
│ │ │ resourceHashesCache.bin
│ │ │
│ │ └─vcsMetadata
│ ├─buildOutputCleanup
│ │ buildOutputCleanup.lock
│ │ cache.properties
│ │
│ ├─kotlin
│ │ └─errors
│ └─vcs-1
│ gc.properties
│
├─.idea
│ .gitignore
│ .name
│ compiler.xml
│ gradle.xml
│ kotlinc.xml
│ migrations.xml
│ misc.xml
│ other.xml
│ workspace.xml
│
├─app
│ │ .gitignore
│ │ build.gradle.kts
│ │ proguard-rules.pro
│ │
│ └─src
│ ├─androidTest
│ │ └─java
│ │ └─only
│ │ └─fuck
│ │ └─android
│ │ ExampleInstrumentedTest.kt
│ │
│ ├─main
│ │ │ AndroidManifest.xml
│ │ │
│ │ ├─java
│ │ │ └─only
│ │ │ └─fuck
│ │ │ └─android
│ │ └─res
│ │ ├─drawable
│ │ │ ic_launcher_background.xml
│ │ │ ic_launcher_foreground.xml
│ │ │
│ │ ├─mipmap-anydpi
│ │ │ ic_launcher.xml
│ │ │ ic_launcher_round.xml
│ │ │
│ │ ├─mipmap-hdpi
│ │ │ ic_launcher.webp
│ │ │ ic_launcher_round.webp
│ │ │
│ │ ├─mipmap-mdpi
│ │ │ ic_launcher.webp
│ │ │ ic_launcher_round.webp
│ │ │
│ │ ├─mipmap-xhdpi
│ │ │ ic_launcher.webp
│ │ │ ic_launcher_round.webp
│ │ │
│ │ ├─mipmap-xxhdpi
│ │ │ ic_launcher.webp
│ │ │ ic_launcher_round.webp
│ │ │
│ │ ├─mipmap-xxxhdpi
│ │ │ ic_launcher.webp
│ │ │ ic_launcher_round.webp
│ │ │
│ │ ├─values
│ │ │ colors.xml
│ │ │ strings.xml
│ │ │ themes.xml
│ │ │
│ │ ├─values-night
│ │ │ themes.xml
│ │ │
│ │ └─xml
│ │ backup_rules.xml
│ │ data_extraction_rules.xml
│ │
│ └─test
│ └─java
│ └─only
│ └─fuck
│ └─android
│ ExampleUnitTest.kt
│
└─gradle
│ libs.versions.toml
│
└─wrapper
gradle-wrapper.jar
gradle-wrapper.properties
PlumeFileTree.md
- .gitignore
- build.gradle.kts
- gradle.properties
- gradlew
- gradlew.bat
- local.properties
- settings.gradle.kts
- .gradle
- config.properties
- file-system.probe
- 8.7
- gc.properties
- checksums
- checksums.lock
- md5-checksums.bin
- sha1-checksums.bin
- dependencies-accessors
- gc.properties
- e5d0c485ed9081f602aa0c935b664eb63f940590
- metadata.bin
- classes
- org
- gradle
- accessors
- dm
- LibrariesForLibs$AndroidPluginAccessors.class
- LibrariesForLibs$AndroidxCoreLibraryAccessors.class
- LibrariesForLibs$AndroidxEspressoLibraryAccessors.class
- LibrariesForLibs$AndroidxLibraryAccessors.class
- LibrariesForLibs$BundleAccessors.class
- LibrariesForLibs$JetbrainsKotlinPluginAccessors.class
- LibrariesForLibs$JetbrainsPluginAccessors.class
- LibrariesForLibs$PluginAccessors.class
- LibrariesForLibs$VersionAccessors.class
- LibrariesForLibs.class
- LibrariesForLibsInPluginsBlock$AndroidPluginAccessors.class
- LibrariesForLibsInPluginsBlock$AndroidxCoreLibraryAccessors.class
- LibrariesForLibsInPluginsBlock$AndroidxEspressoLibraryAccessors.class
- LibrariesForLibsInPluginsBlock$AndroidxLibraryAccessors.class
- LibrariesForLibsInPluginsBlock$BundleAccessors.class
- LibrariesForLibsInPluginsBlock$JetbrainsKotlinPluginAccessors.class
- LibrariesForLibsInPluginsBlock$JetbrainsPluginAccessors.class
- LibrariesForLibsInPluginsBlock$PluginAccessors.class
- LibrariesForLibsInPluginsBlock$VersionAccessors.class
- LibrariesForLibsInPluginsBlock.class
- sources
- org
- gradle
- accessors
- dm
- LibrariesForLibs.java
- LibrariesForLibsInPluginsBlock.java
- executionHistory
- executionHistory.lock
- expanded
- fileChanges
- last-build.bin
- fileHashes
- fileHashes.bin
- fileHashes.lock
- resourceHashesCache.bin
- vcsMetadata
- buildOutputCleanup
- buildOutputCleanup.lock
- cache.properties
- kotlin
- errors
- vcs-1
- gc.properties
- .idea
- .gitignore
- .name
- compiler.xml
- gradle.xml
- kotlinc.xml
- migrations.xml
- misc.xml
- other.xml
- workspace.xml
- app
- .gitignore
- build.gradle.kts
- proguard-rules.pro
- src
- androidTest
- java
- only
- fuck
- android
- ExampleInstrumentedTest.kt
- main
- AndroidManifest.xml
- java
- only
- fuck
- android
- res
- drawable
- ic_launcher_background.xml
- ic_launcher_foreground.xml
- mipmap-anydpi
- ic_launcher.xml
- ic_launcher_round.xml
- mipmap-hdpi
- ic_launcher.webp
- ic_launcher_round.webp
- mipmap-mdpi
- ic_launcher.webp
- ic_launcher_round.webp
- mipmap-xhdpi
- ic_launcher.webp
- ic_launcher_round.webp
- mipmap-xxhdpi
- ic_launcher.webp
- ic_launcher_round.webp
- mipmap-xxxhdpi
- ic_launcher.webp
- ic_launcher_round.webp
- values
- colors.xml
- strings.xml
- themes.xml
- values-night
- themes.xml
- xml
- backup_rules.xml
- data_extraction_rules.xml
- test
- java
- only
- fuck
- android
- ExampleUnitTest.kt
- gradle
- libs.versions.toml
- wrapper
- gradle-wrapper.jar
- gradle-wrapper.properties