88 "os"
99 "path/filepath"
1010 "slices"
11+ "strconv"
1112 "strings"
1213 "unicode"
1314 "unicode/utf8"
@@ -136,12 +137,14 @@ func (r *renderer) render() ([]byte, error) {
136137}
137138
138139func (r * renderer ) renderFrontMatter () {
139- info := r . c . Book . Description . TitleInfo
140- title := strings . TrimSpace ( info . BookTitle . Value )
141- if title == "" {
142- title = strings . TrimSuffix ( r . c . SrcName , ".fb2" )
140+ if r . format == formatMD {
141+ r . renderMarkdownFrontMatter ( )
142+ r . heading ( r . plainInline ( r . bookTitle ()), 1 )
143+ return
143144 }
144- r .heading (r .plainInline (title ), 1 )
145+
146+ info := r .c .Book .Description .TitleInfo
147+ r .heading (r .plainInline (r .bookTitle ()), 1 )
145148
146149 lines := make ([]string , 0 , 6 )
147150 if authors := formatAuthors (info .Authors ); authors != "" {
@@ -162,6 +165,111 @@ func (r *renderer) renderFrontMatter() {
162165 r .paragraph (strings .Join (lines , "\n " ))
163166}
164167
168+ func (r * renderer ) bookTitle () string {
169+ title := strings .TrimSpace (r .c .Book .Description .TitleInfo .BookTitle .Value )
170+ if title == "" {
171+ return strings .TrimSuffix (r .c .SrcName , ".fb2" )
172+ }
173+ return title
174+ }
175+
176+ func (r * renderer ) renderMarkdownFrontMatter () {
177+ info := r .c .Book .Description .TitleInfo
178+ lines := []string {"---" }
179+ if title := r .markdownMetaTitle (); title != "" {
180+ lines = append (lines , "title: " + yamlScalar (title ))
181+ }
182+ if authors := r .markdownMetaAuthors (info .Authors ); len (authors ) > 0 {
183+ lines = append (lines , "authors:" )
184+ for _ , author := range authors {
185+ lines = append (lines , " - " + yamlScalar (author ))
186+ }
187+ }
188+ if len (info .Sequences ) > 0 {
189+ lines = append (lines , "series:" )
190+ for _ , seq := range info .Sequences {
191+ name := strings .TrimSpace (seq .Name )
192+ if name == "" {
193+ continue
194+ }
195+ lines = append (lines , " - name: " + yamlScalar (name ))
196+ if seq .Number != nil {
197+ lines = append (lines , " number: " + strconv .Itoa (* seq .Number ))
198+ }
199+ }
200+ }
201+ if lang := strings .TrimSpace (info .Lang .String ()); lang != "" && lang != "und" {
202+ lines = append (lines , "language: " + yamlScalar (lang ))
203+ }
204+ if date := formatDate (info .Date ); date != "" {
205+ lines = append (lines , "date: " + yamlScalar (date ))
206+ }
207+ if genres := markdownMetaGenres (info .Genres ); len (genres ) > 0 {
208+ lines = append (lines , "genres:" )
209+ for _ , genre := range genres {
210+ lines = append (lines , " - " + yamlScalar (genre ))
211+ }
212+ }
213+ lines = append (lines , "---" )
214+ r .block (strings .Join (lines , "\n " ))
215+ }
216+
217+ func (r * renderer ) markdownMetaTitle () string {
218+ templateText := strings .TrimSpace (r .cfg .Metainformation .TitleTemplate )
219+ if templateText != "" {
220+ title , err := r .c .Book .ExpandTemplateMetainfo (config .MetaTitleTemplateFieldName , templateText , r .c .SrcName , r .c .OutputFormat )
221+ if err == nil && strings .TrimSpace (title ) != "" {
222+ return strings .TrimSpace (title )
223+ }
224+ }
225+ title := strings .TrimSpace (r .c .Book .Description .TitleInfo .BookTitle .Value )
226+ if title == "" {
227+ return strings .TrimSuffix (r .c .SrcName , ".fb2" )
228+ }
229+ return title
230+ }
231+
232+ func (r * renderer ) markdownMetaAuthors (authors []fb2.Author ) []string {
233+ result := make ([]string , 0 , len (authors ))
234+ templateText := strings .TrimSpace (r .cfg .Metainformation .CreatorNameTemplate )
235+ for i := range authors {
236+ name := ""
237+ if templateText != "" {
238+ expanded , err := r .c .Book .ExpandTemplateAuthorName (
239+ config .MetaCreatorNameTemplateFieldName ,
240+ templateText ,
241+ r .c .OutputFormat ,
242+ i ,
243+ & authors [i ],
244+ )
245+ if err == nil {
246+ name = strings .TrimSpace (expanded )
247+ }
248+ }
249+ if name == "" {
250+ name = formatAuthor (authors [i ])
251+ }
252+ if name != "" {
253+ result = append (result , name )
254+ }
255+ }
256+ return result
257+ }
258+
259+ func markdownMetaGenres (genres []fb2.GenreRef ) []string {
260+ result := make ([]string , 0 , len (genres ))
261+ for _ , genre := range genres {
262+ if text := strings .TrimSpace (genre .Value ); text != "" {
263+ result = append (result , text )
264+ }
265+ }
266+ return result
267+ }
268+
269+ func yamlScalar (text string ) string {
270+ return strconv .Quote (text )
271+ }
272+
165273func (r * renderer ) renderAnnotation () {
166274 r .heading (r .plainInline (annotationTitle (r .cfg )), 2 )
167275 r .renderFlow (r .c .Book .Description .TitleInfo .Annotation .Items , 0 , blockNormal )
0 commit comments