--- loncom/homework/structuretags.pm 2011/12/12 12:07:45 1.497.2.3 +++ loncom/homework/structuretags.pm 2011/12/14 11:56:08 1.497.2.4 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # definition of tags that give a structure to a document # -# $Id: structuretags.pm,v 1.497.2.3 2011/12/12 12:07:45 foxr Exp $ +# $Id: structuretags.pm,v 1.497.2.4 2011/12/14 11:56:08 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -69,6 +69,86 @@ BEGIN { &Apache::lonxml::register('Apache::structuretags',('block','languageblock','translated','instructorcomment','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','problemtype','startpartmarker','startouttext','endpartmarker','endouttext','simpleeditbutton','definetag')); } + +#--------------------------------------------------------------------------------- +# +# This section of code deals with hyphenation management. +# We must do three things: +# - keep track fo the desired languages to alter the header. +# - provide hyphenation selection as needed by each language that appears in the +# text. +# - Provide the header text needed to make available the desired hyphenations. +# +# + +# Hash whose keys are the languages encountered in the document/resource. +# + +my %languages_required; +## +# Given a language selection as input returns a chunk of LaTeX that +# selects the required hyphenator. +# +# @param language - the language being selected. +# @return string +# @retval The LaTeX needed to select the hyphenation appropriate to the language. +# +sub select_hyphenation { + my $language = shift; + + $language = &Apache::loncommon::latex_language($language); # Translate -> latex language. + + # If there is no latex language there's not much we can do: + + if ($language) { + &require_language($language); + my $babel_hyphenation = "\\select_language{$language}"; + + return $babel_hyphenation; + } else { + return ''; + } +} +## +# Clears the set of languages required by the document being rendered. +# +sub clear_required_languages { + %languages_required = (); +} +## +# Allows an external client of this module to register a need for a language: +# +# @param LaTeX language required: +# +sub require_language { + my $language = shift; + + $languages_required{$language} = 1; +} + +## +# Provides the header for babel that indicates the languages +# the document requires. +# @return string +# @retval \usepackage[lang1,lang2...]{babel} +# @retval '' if there are no languages_required. +sub languages_header { + + my $header =''; + my @languages = keys(%languages_required); + + # Only generate the header if there are languages: + + if (scalar @languages) { + my $language_list = join(',', (@languages)); + $header = '\usepackage['.$language_list."]{babel}\n"; + } + + return $header; +} + +#---------------------------------------------------------------------------------- + sub start_web { my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_; if ($target ne 'edit' && $target ne 'modified') { @@ -1581,14 +1661,15 @@ sub start_languageblock { # Now loop over all languages in order of preference + my $render; foreach my $preferred_language (@preferred_languages) { # If neither include/nor exlude is present the block is going # to get rendered. - my $render=1; my $found=0; - + $render=1; + # If include is specified, don't render the block # unless the preferred language is included in the set. @@ -1716,7 +1797,10 @@ sub end_languageblock { my @possibilities = keys(%available_texts); my $which = &Apache::loncommon::languages(\@possibilities) || 'default'; - $result = $available_texts{$which}; + if ($target eq 'tex') { + $result = &select_hyphenation($which); + } + $result .= $available_texts{$which}; } undef(%available_texts); &Apache::lonxml::deregister('Apache::structuretags',('lang'));