Skip to content

Commit

Permalink
update to FW 3.7 structure
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Sep 1, 2019
1 parent d80d424 commit d2a7d2d
Show file tree
Hide file tree
Showing 38 changed files with 517 additions and 121 deletions.
13 changes: 13 additions & 0 deletions fiduswriter/book/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
from . import models


class BookStyleFileInline(admin.TabularInline):
model = models.BookStyleFile


class BookStyleAdmin(admin.ModelAdmin):
inlines = [
BookStyleFileInline,
]


admin.site.register(models.BookStyle, BookStyleAdmin)


class BookAdmin(admin.ModelAdmin):
pass

Expand Down
263 changes: 263 additions & 0 deletions fiduswriter/book/fixtures/initial_data.json

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
52 changes: 52 additions & 0 deletions fiduswriter/book/migrations/0007_bookstyle_bookstylefile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 2.2.4 on 2019-08-31 22:40
import os
import book.models
from django.db import migrations, models
import django.db.models.deletion
from django.core.management import call_command


fixture_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '../fixtures'))
fixture_filename = 'initial_data.json'


def load_fixture(apps, schema_editor):
fixture_file = os.path.join(fixture_dir, fixture_filename)
call_command('loaddata', fixture_file)


def unload_fixture(apps, schema_editor):
BookStyle = apps.get_model("book", "BookStyle")
BookStyle.objects.all().delete()


class Migration(migrations.Migration):

dependencies = [
('book', '0006_auto_20190622_2126'),
]

operations = [
migrations.CreateModel(
name='BookStyle',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(default='Default', help_text='The human readable title.', max_length=128)),
('slug', models.SlugField(default='default', help_text='The base of the filenames the style occupies.', max_length=20, unique=True)),
('contents', models.TextField(default='', help_text='The CSS style definiton.')),
],
),
migrations.CreateModel(
name='BookStyleFile',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('file', models.FileField(help_text='A file references in the style. The filename will be replaced with the final url of the file in the style.', upload_to=book.models.bookstylefile_location)),
('filename', models.CharField(help_text='The original filename.', max_length=255)),
('style', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='book.BookStyle')),
],
options={
'unique_together': {('filename', 'style')},
},
),
migrations.RunPython(load_fixture, reverse_code=unload_fixture),
]
54 changes: 54 additions & 0 deletions fiduswriter/book/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,57 @@ class Meta(object):
def __str__(self):
return self.user.readable_name + \
' (' + self.rights + ') on ' + self.book.title


class BookStyle(models.Model):
title = models.CharField(
max_length=128,
help_text='The human readable title.',
default='Default'
)
slug = models.SlugField(
max_length=20,
help_text='The base of the filenames the style occupies.',
default='default',
unique=True
)
contents = models.TextField(
help_text='The CSS style definiton.',
default=''
)

def __str__(self):
return self.title


def bookstylefile_location(instance, filename):
# preserve the original filename
instance.filename = filename
return '/'.join(['book-style-files', filename])


class BookStyleFile(models.Model):
file = models.FileField(
upload_to=bookstylefile_location,
help_text=(
'A file references in the style. The filename will be replaced '
'with the final url of the file in the style.'
)
)
filename = models.CharField(
max_length=255,
help_text='The original filename.'
)
style = models.ForeignKey(
'BookStyle',
on_delete=models.deletion.CASCADE
)

def __str__(self):
return self.filename + ' of ' + self.style.title

def natural_key(self):
return (self.file.url, self.filename)

class Meta(object):
unique_together = (("filename", "style"),)
18 changes: 9 additions & 9 deletions fiduswriter/book/static/js/modules/books/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export class BookActions {
{id}
).catch(
error => {
addAlert('error', gettext(`${gettext('Could not delete book')}: '${book.title}'`))
addAlert('error', `${gettext('Could not delete book')}: '${book.title}'`)
throw (error)
}
).then(()=> {
addAlert('success', gettext(`${gettext('Book has been deleted')}: '${book.title}'`))
addAlert('success', `${gettext('Book has been deleted')}: '${book.title}'`)
this.bookOverview.removeTableRows([id])
this.bookOverview.bookList = this.bookOverview.bookList.filter(book => book.id !== id)
})
Expand All @@ -42,7 +42,7 @@ export class BookActions {
classes: "fw-dark",
click: () => {
ids.forEach(id => this.deleteBook(parseInt(id)))
addAlert('success', gettext('The book(s) have been deleted'))
addAlert('success', ids.length > 1 ? gettext('The books have been deleted') : gettext('The book has been deleted'))
dialog.close()
}
},
Expand All @@ -55,7 +55,7 @@ export class BookActions {
title: gettext('Confirm deletion'),
id: 'confirmdeletion',
icon: 'exclamation-triangle',
body: `<p>${gettext('Delete the book(s)?')}</p>`,
body: `<p>${ids.length > 1 ? gettext('Delete the books?') : gettext('Delete the book?')}</p>`,
buttons
})
dialog.open()
Expand Down Expand Up @@ -182,8 +182,8 @@ export class BookActions {
},
settings: {
bibliography_header: gettext('Bibliography'),
citationstyle: this.bookOverview.styles.citation_styles[0].filename,
documentstyle: this.bookOverview.styles.document_styles[0].filename,
citationstyle: 'apa',
book_style: this.bookOverview.styles[0] ? this.bookOverview.styles[0].slug : false,
papersize: 'octavo'
}
}
Expand All @@ -206,7 +206,7 @@ export class BookActions {
book,
documentList: this.bookOverview.documentList,
citationDefinitions: this.bookOverview.styles.citation_styles,
documentStyleList: this.bookOverview.styles.document_styles,
bookStyleList: this.bookOverview.styles,
imageDB: {db: Object.assign({}, imageDB.db, bookImageDB.db)}
})

Expand Down Expand Up @@ -405,8 +405,8 @@ export class BookActions {
book.settings.citationstyle = event.target.value
})

dialog.dialogEl.querySelector('#book-settings-documentstyle').addEventListener('change', event => {
book.settings.documentstyle = event.target.value
dialog.dialogEl.querySelector('#book-settings-bookstyle').addEventListener('change', event => {
book.settings.book_style = event.target.value
})

dialog.dialogEl.querySelector('#book-settings-papersize').addEventListener('change', event => {
Expand Down
31 changes: 24 additions & 7 deletions fiduswriter/book/static/js/modules/books/exporter/epub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import {addAlert} from "../../../common"


export class EpubBookExporter extends DOMExporter {
constructor(schema, staticUrl, citationStyles, citationLocales, documentStyles, book, user, docList) {
super(schema, staticUrl, citationStyles, citationLocales, documentStyles)
constructor(schema, staticUrl, csl, bookStyles, book, user, docList) {
super(schema, staticUrl, csl, bookStyles)
this.book = book
this.user = user
this.docList = docList
Expand All @@ -42,6 +42,22 @@ export class EpubBookExporter extends DOMExporter {
)
}

addBookStyle(doc) {
const bookStyle = this.documentStyles.find(bookStyle => bookStyle.slug===this.book.settings.book_style)
if (!bookStyle) {
return false
}
// The files will be in the base directory. The filenames of
// BookStyleFiles will therefore not need to replaced with their URLs.

this.styleSheets.push({contents: bookStyle.contents, filename: `${bookStyle.slug}.css`})
this.fontFiles = this.fontFiles.concat(bookStyle.bookstylefile_set.map(([url, filename]) => ({
filename,
url
})))
return `${bookStyle.slug}.css`
}

exportOne() {
this.book.chapters.sort((a, b) => a.number > b.number ? 1 : -1)

Expand Down Expand Up @@ -129,9 +145,7 @@ export class EpubBookExporter extends DOMExporter {
this.book.settings.citationstyle,
bibliographyHeader,
{db: chapter.doc.bibliography},
this.citationStyles,
this.citationLocales,
true
this.csl
)
return citRenderer.init().then(
() => {
Expand All @@ -152,11 +166,14 @@ export class EpubBookExporter extends DOMExporter {
}

exportTwo() {

const bookStyle = this.addBookStyle()
this.outputList = this.outputList.concat(
this.chapters.map(chapter => {
chapter.contents = styleEpubFootnotes(chapter.contents)
const styleSheets = [{filename: 'document.css'}, {filename: this.addDocStyle(chapter.doc)}]
const styleSheets = [{filename: 'document.css'}]
if (bookStyle) {
styleSheets.push({filename: bookStyle})
}
let xhtmlCode = xhtmlTemplate({
part: chapter.part,
shortLang: chapter.doc.settings.language.split('-')[0],
Expand Down
32 changes: 24 additions & 8 deletions fiduswriter/book/static/js/modules/books/exporter/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import download from "downloadjs"
import {DOMSerializer} from "prosemirror-model"

export class HTMLBookExporter extends DOMExporter {
constructor(schema, staticUrl, citationStyles, citationLocales, documentStyles, book, user, docList) {
super(schema, staticUrl, citationStyles, citationLocales, documentStyles)
constructor(schema, staticUrl, csl, bookStyles, book, user, docList) {
super(schema, staticUrl, csl, bookStyles)
this.book = book
this.user = user
this.docList = docList
Expand All @@ -35,6 +35,22 @@ export class HTMLBookExporter extends DOMExporter {
)
}

addBookStyle(doc) {
const bookStyle = this.documentStyles.find(bookStyle => bookStyle.slug===this.book.settings.book_style)
if (!bookStyle) {
return false
}
// The files will be in the base directory. The filenames of
// BookStyleFiles will therefore not need to replaced with their URLs.

this.styleSheets.push({contents: bookStyle.contents, filename: `${bookStyle.slug}.css`})
this.fontFiles = this.fontFiles.concat(bookStyle.bookstylefile_set.map(([url, filename]) => ({
filename,
url
})))
return `${bookStyle.slug}.css`
}

exportOne() {

this.chapters = this.book.chapters.sort(
Expand Down Expand Up @@ -69,9 +85,7 @@ export class HTMLBookExporter extends DOMExporter {
this.book.settings.citationstyle,
bibliographyHeader,
{db: chapter.doc.bibliography},
this.citationStyles,
this.citationLocales,
true
this.csl
)
return citRenderer.init().then(
() => {
Expand All @@ -96,14 +110,16 @@ export class HTMLBookExporter extends DOMExporter {
}

exportTwo() {
const bookStyle = this.addBookStyle()
let contentItems = []

let outputList = this.chapters.map((chapter, index) => {
const contents = chapter.contents,
doc = chapter.doc,
title = doc.title,
styleSheets = [{filename: this.addDocStyle(doc)}]

styleSheets = []
if (bookStyle) {
styleSheets.push({filename: bookStyle})
}
this.prepareBinaryFiles(contents)

if (this.book.chapters[index].part && this.book.chapters[index].part !== '') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class LatexBookExporter {
})
// Start a converter, only for creating a preamble/epilogue that combines
// the features of all of the contained chapters.
const bookConverter = new LatexExporterConvert(this, {db: combinedImages}, {db: combinedBibliography})
const bookConverter = new LatexExporterConvert(this, {db: combinedImages}, {db: combinedBibliography}, {language: 'en-US', bibliography_header: {}})
bookConverter.features = features
const preamble = bookConverter.assemblePreamble()
const epilogue = bookConverter.assembleEpilogue()
Expand Down
24 changes: 13 additions & 11 deletions fiduswriter/book/static/js/modules/books/exporter/print/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const CSS_PAPER_SIZES = {

export class PrintBookExporter extends HTMLBookExporter {

constructor(schema, staticUrl, citationStyles, citationLocales, documentStyles, book, user, docList) {
super(schema, staticUrl, citationStyles, citationLocales, documentStyles, book, user, docList)
constructor(schema, staticUrl, csl, documentStyles, book, user, docList) {
super(schema, staticUrl, csl, documentStyles, book, user, docList)
this.chapterTemplate = chapterTemplate
}

Expand Down Expand Up @@ -79,15 +79,17 @@ export class PrintBookExporter extends HTMLBookExporter {
max-width: 100%;
}
`
const docStyle = this.documentStyles.find(style => style.filename === this.book.settings.documentstyle)
css += `
${docStyle.fonts.map(font => {
return `@font-face {${
font[1].replace('[URL]', font[0])
}}`
}).join('\n')}
${docStyle.contents}
`
const bookStyle = this.documentStyles.find(style => style.slug === this.book.settings.book_style)
if (bookStyle) {
let contents = bookStyle.contents
bookStyle.bookstylefile_set.forEach(
([url, filename]) => contents = contents.replace(
new RegExp(filename, 'g'),
url
)
)
css += contents
}

return css
}
Expand Down
Loading

0 comments on commit d2a7d2d

Please sign in to comment.