Move implements of encode to generate_to_file.
Update docs.