BirjuVachhani / spider

A small dart library to generate Assets dart code from assets folder.
https://spider.birju.dev/
Apache License 2.0
190 stars 20 forks source link

Asset Documentation. #72

Open Chaitanyabsprip opened 1 year ago

Chaitanyabsprip commented 1 year ago

Is your feature request related to a problem? Please describe. It's difficult to figure out whether some asset already exists. It would be great to have a markdown file with preview for all image assets.

Describe the solution you'd like I am already using spider to generate the constants file. It would be nice if this functionality could be added to spider itself.

Describe alternatives you've considered I created a small python script that generates a markdown file with preview for all image assets.

BirjuVachhani commented 1 year ago

@Chaitanyabsprip I don't know if this should be a part of the package. Yes it would be convenient to see a markdown file. Or you can just open finder/explorer and see what images are there. IDEs give an option to open a directory with file manager so it would be pretty straight-forward to do so. Let me know if you have usages other than this. For now, I am keeping this issue open so it can be discussed further if required.

I'd love to see your script though. Would you mind sharing it here?

Chaitanyabsprip commented 1 year ago

I don't use an IDE, I use neovim, so I don't have direct preview for the assets except for the generated markdown files. Preview in markdown is even though not necessary, it is a nice to have.

My script

import os

def find_files_with_ext(directory: str, ext:str):
    files_with_ext = []
    for root, _, files in os.walk(directory):
        for file in files:
            if file.endswith(ext):
                files_with_ext.append(os.path.join(root, file))
    return files_with_ext

def generate_line(filename:str):
    hyperlink = f"![](../{filename})"
    line = f"{hyperlink}\n*{filename}*\n"
    return f'{line}\n'

def write_to_output_file(output_file: str, files: list[str]):
    with open(output_file, 'w') as f:
        for filename in files:
            print(filename)
            f.write(generate_line(filename))

def main():
    directory = 'assets'
    svgs_docs = 'docs/svg_assets.md'
    png_docs = 'docs/png_assets.md'
    svgs = find_files_with_ext(directory, '.svg')
    pngs = find_files_with_ext(directory, '.png')
    write_to_output_file(svgs_docs, svgs)
    write_to_output_file(png_docs, pngs)

main()