Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Custom Print Profiles #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ local/
coverage/
.idea/
changes.log
yarn.lock
.vscode/launch.json
7 changes: 7 additions & 0 deletions New folder/sample_profile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: test_profile
default_max: 30
base: a4
paper_size: letter
lines_per_page: 10
page_width: 4
page_height: 8
2 changes: 1 addition & 1 deletion awc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require.config({
map: {
'modernizr': {}
},
use_node_require: ['jquery', 'fs', 'd3', 'pdfkit', 'aw-parser', 'protoplast', 'lodash']
use_node_require: ['path', 'jquery', 'fs', 'd3', 'pdfkit', 'aw-parser', 'protoplast', 'lodash', 'yamljs']
});

var Bootstrap = require('bootstrap');
Expand Down
21 changes: 20 additions & 1 deletion js/core/model/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ define(function(require) {
var _ = require('lodash'),
Protoplast = require('protoplast'),
fontLoaders = require('fonts/font-loaders'),
PrintProfileUtil = require('utils/print-profile-util'),
fs = require("fs"),
path = require("path"),
print_profiles = require('utils/print-profiles');

// TODO: Decouple plugin-specific settings (+++)
Expand Down Expand Up @@ -231,7 +234,23 @@ define(function(require) {

print: {
get: function() {
var tmpRatio = fontLoaders[this.font_family] && fontLoaders[this.font_family].config["tmp-ratio"];
var tmpRatio = fontLoaders[this.font_family] && fontLoaders[this.font_family].config["tmp-ratio"];
var profileName;

// Load the profile file if it doesn't exist already
this.print_profile = this.print_profile.replace(/[",']/g, "");
profileName = path.basename(this.print_profile);

if (!print_profiles[profileName]) {
var prf = PrintProfileUtil.loadProfile(this.print_profile);

fs.copyFileSync(this.print_profile,
'print-profiles/' + profileName
);

print_profiles[this.print_profile] = prf;
}

var printProfile = print_profiles[this.print_profile];
if (tmpRatio) {
printProfile = _.cloneDeep(print_profiles[this.print_profile]);
Expand Down
4 changes: 3 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var config = require.config({
baseUrl: 'js',
paths: {
print_profiles: '/print-profiles',
samples: '../samples/compiled',
templates: '../templates',
test_screenplays: '../test/data/test_screenplays',
Expand All @@ -25,7 +26,8 @@
'aw-parser': '../node_modules/aw-parser/dist/aw-parser.amd',
'aw-liner': '../node_modules/aw-liner/dist/aw-liner.amd',
text: '../node_modules/requirejs-text/text',
Blob: 'libs/Blob'
Blob: 'libs/Blob',
yamljs: '../node_modules/yamljs/yaml.min'
},
shim: {
handlebars: {
Expand Down
61 changes: 61 additions & 0 deletions js/utils/print-profile-util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,70 @@
define(function(require) {

var _ = require('lodash'),
fs = require('fs'),
YAML = require('yamljs'),
Protoplast = require('protoplast');

var PrintProfileUtil = Protoplast.extend({
loadProfile: function(profileLocation) {
var rawData = fs.readFileSync(profileLocation, 'utf8');
var matchMaxRgx = /default_max:\s*([&A-Za-z]*)\s*(\d+)/;
var matchBaseRgx = /base:\s*[",']?([A-Za-z0-9]*)[",']?/;

var matchBase = rawData.match(matchBaseRgx);
var base = null;
var baseData = {};

if(matchBase != null && matchBase.length > 1) {
// get contents of base file
base = fs.readFileSync('print-profiles/' + matchBase[1] + '.yml', 'utf8');
// replace default_max in base file with the new file
var baseDefMax = base.match(matchMaxRgx);
var srcDefMax = rawData.match(matchMaxRgx);
if (baseDefMax) {
var newString = "default_max: " +
(baseDefMax[1] != null && baseDefMax[1] == null ? "" : baseDefMax[1]) +
" " +
srcDefMax[2];

base = base.replace(baseDefMax[0], newString);

}

baseData = YAML.parse(base);
profileData = Object.assign(baseData, YAML.parse(rawData));
} else {
profileData = YAML.parse(rawData);
}

return profileData;
},
/**
*
* @param {number} defaultMax
* @param {object} source
* @param {NULL|object} base
* @returns {object}
*/
loadFiles: function() {
var dir = 'print-profiles';
var result = {};
try {
var files = fs.readdirSync(dir, { withFileTypes: true });
var $vm = this;
files.forEach(function(file) {
if (file.isFile() && file.name.indexOf('.yml')) {
var profile = $vm.loadProfile(dir + "/" + file.name);
result[profile.name] = profile;
}
});

} catch (err) {
console.log(err);
}

return result;
},

/**
* Create new profile based on a given sourceProfile and change the font size
Expand Down
101 changes: 7 additions & 94 deletions js/utils/print-profiles.js
Original file line number Diff line number Diff line change
@@ -1,105 +1,18 @@
define(function(require) {
define(function(require) {

var browser = require('utils/browser'),
PrintProfileUtil = require('utils/print-profile-util');
PrintProfileUtil = require('utils/print-profile-util');

var A4_DEFAULT_MAX = 58,
US_DEFAULT_MAX = 61;

var print_profiles = {
"a4": {
paper_size: "a4",
font_size: 12,
lines_per_page: 57,
top_margin: 1.0,
page_width: 8.27,
page_height: 11.7,
left_margin: 1.5,
right_margin: 1,
font_width: 0.1,
font_height: 0.1667,
line_spacing: 1,
page_number_top_margin: 0.5,
dual_max_factor: 0.75,
title_page: {
top_start: 3.5,
left_side: ['notes', 'copyright'],
right_side: ['draft date', 'date', 'contact']
},
scene_heading: {
feed: 1.5,
max: A4_DEFAULT_MAX
},
action: {
feed: 1.5,
max: A4_DEFAULT_MAX
},
shot: {
feed: 1.5,
max: A4_DEFAULT_MAX
},
character: {
feed: 3.5,
max: 33
},
parenthetical: {
feed: 3,
max: 26
},
dialogue: {
feed: 2.5,
max: 36
},
transition: {
feed: 0.0,
max: A4_DEFAULT_MAX
},
centered: {
feed: 1.5,
style: 'center',
max: A4_DEFAULT_MAX
},
synopsis: {
feed: 0.5,
max: A4_DEFAULT_MAX,
italic: true,
color: '#888888',
padding: 0,
feed_with_last_section: true
},
section: {
feed: 0.5,
max: A4_DEFAULT_MAX,
color: '#555555',
level_indent: 0.2
},
note: {
color: '#888888',
italic: true
}
}
};

print_profiles.usletter = JSON.parse(JSON.stringify(print_profiles.a4));
var letter = print_profiles.usletter;
letter.paper_size = 'letter';
letter.lines_per_page = 55;
letter.page_width = 8.5;
letter.page_height = 11;

letter.scene_heading.max = US_DEFAULT_MAX;
letter.action.max = US_DEFAULT_MAX;
letter.shot.max = US_DEFAULT_MAX;
letter.transition.max = US_DEFAULT_MAX;
letter.section.max = US_DEFAULT_MAX;
letter.synopsis.max = US_DEFAULT_MAX;
var print_profiles = PrintProfileUtil.loadFiles();

// font size = experimental feature
var url_params = browser.url_params();
if (url_params.fontSize) {
var fontSize = parseInt(url_params.fontSize, 10);
print_profiles.usletter = PrintProfileUtil.withNewFontSize(print_profiles.usletter, fontSize);
print_profiles.a4 = PrintProfileUtil.withNewFontSize(print_profiles.a4, fontSize);
Object.keys(print_profiles, function(profile) {
var prf = print_profiles[profile];
print_profiles[profile] = ProcessingInstruction.withNewFontSize(prf, fontSize);
});
}

return print_profiles;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@
"pdfkit": "^0.11.0",
"protoplast": "2.0.3",
"requirejs-text": "^2.0.15",
"stdio": "^0.2.7"
"stdio": "^0.2.7",
"yamljs": "^0.3.0"
},
"snyk": true
}
76 changes: 76 additions & 0 deletions print-profiles/a4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: a4
default_max: &max 61
paper_size: a4
font_size: 12
lines_per_page: 57
top_margin: 1.0
page_width: 8.27
page_height: 11.7
left_margin: 1.5
right_margin: 1
font_width: 0.1
font_height: 0.1667
line_spacing: 1
page_number_top_margin: 0.5
dual_max_factor: 0.75

title_page:
top_start: 3.5
left_side:
- notes
- copyright
right_side:
- draft date
- date
- contact

scene_heading:
feed: 1.5
max: *max

action:
feed: 1.5
max: *max

shot:
feed: 1.5
max: *max

character:
feed: 3.5
max: 33

parenthetical:
feed: 3
max: 26

dialogue:
feed: 2.5
max: 36

transition:
feed: 0.0
max: *max

centered:
feed: 1.5
style: 'center'
max: *max

synopsis:
feed: 0.5
max: *max
italic: true
color: '#888888'
padding: 0
feed_with_last_section: true

section:
feed: 0.5
max: *max
color: '#555555'
level_indent: 0.2

note:
color: '#888888'
italic: true
7 changes: 7 additions & 0 deletions print-profiles/letter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: usletter
default_max: 58
base: a4
paper_size: letter
lines_per_page: 55
page_width: 8.5
page_height: 11
Loading