/** * Is this point far enough from the end to insert? * * Blocks insertion if the current insertion point is less than the minimum * distance from the end of the content defined in the "from_end" option, * as measured in the usual units of distance (paragraphs/words/characters). * * If an integer is passed, it's treated as a unit of "paragraphs". * */ public static function meets_minimum_distance_from_end($can_insert, $context, $args, $already_inserted) { if (!isset($args['from_end'])) { return $can_insert; } if (is_array($args['from_end'])) { $from_end = array_slice($context['parts'], $context['index'] + 1); foreach (array('paragraphs', 'words', 'characters') as $unit) { if (isset($args['from_end'][$unit]) && Comparison::content_less_than($unit, $args['from_end'][$unit], $from_end)) { $can_insert = false; } } } if (is_int($args['from_end'])) { if ($args['from_end'] >= $context['total_paragraphs'] - $context['index']) { $can_insert = false; } } return $can_insert; }
/** * Is this speed bump far enough away from others to insert here? * * Blocks a speed bump from being inserted if it doesn't mean the * distance defined in the speed bump's 'from_speedbump' registration * arguments. */ public static function meets_minimum_distance_from_other_inserts($can_insert, $context, $args, $already_inserted) { // Support passing an integer here, which will be treated as a unit of "paragraphs" if (is_int($args['from_speedbump'])) { $args['from_speedbump'] = array('paragraphs' => $args['from_speedbump']); } if (!is_array($args['from_speedbump'])) { return $can_insert; } if (is_int($args['from_speedbump'])) { $base_distance_constraints = array('paragraphs' => $args['from_speedbump']); $from_speed_bump = array(); } else { $defaults = array('paragraphs' => 1, 'words' => null, 'characters' => null); $base_distance_constraints = array_intersect_key((array) $args['from_speedbump'], $defaults); $from_speedbump = array_diff_key($args['from_speedbump'], $defaults); } $this_paragraph_index = $context['index']; if (count($already_inserted)) { foreach ($already_inserted as $speed_bump) { $distance_constraints = $base_distance_constraints; if (isset($from_speedbump[$speed_bump['speed_bump_id']]) && is_array($from_speedbump[$speed_bump['speed_bump_id']])) { foreach (array('paragraphs', 'words', 'characters') as $unit) { if (isset($from_speedbump[$speed_bump['speed_bump_id']][$unit])) { $distance_constraints[$unit] = $from_speedbump[$speed_bump['speed_bump_id']][$unit]; } } } $distance = Text::content_between_points($context['parts'], $speed_bump['index'], $context['index']); foreach ($distance_constraints as $unit => $measurement) { if (isset($args['from_speedbump'][$speed_bump['speed_bump_id']]) && isset($args['from_speedbump'][$speed_bump['speed_bump_id']][$unit])) { $measurement = $args['from_speedbump'][$speed_bump['speed_bump_id']][$unit]; } if ($measurement && Comparison::content_less_than($unit, $measurement, $distance)) { $can_insert = false; } } } } return $can_insert; }